3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef _MESH_EVENT_HANDLER_H_
21 #define _MESH_EVENT_HANDLER_H_
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
40 // TODO: Add more mesh event if required.
51 META_RECONN_SUCCESSFUL,
70 CHANNEL_DATA_RECIEVED,
76 MAX_EVENT // Maximum event enum
79 extern const char *event_status[];
81 /// mesh event UDP packet
82 typedef struct mesh_event_payload {
84 mesh_event_t mesh_event;
86 uint32_t payload_length;
87 } mesh_event_payload_t;
90 pthread_mutex_t mutex;
95 /// callback for handling the mesh event
96 /** mesh event callback called from wait_for_event() if the mesh event UDP server gets a mesh event.
98 * @param mesh_event_packet packet containing client-id, mesh event & payload (if any).
100 typedef bool (*mesh_event_callback_t)(mesh_event_payload_t mesh_event_packet);
102 /// Creates an UDP server for listening mesh events.
103 /** This function creates an UDP socket, binds it with given interface address and returns a NULL
104 * terminated string containing server's IP address & port number.
106 * @param ifname Name of the network interface to which the socket has to be created.
108 * @return This function returns a NULL terminated string which has IP address and
109 * port number of the server socket. The application should call free() after
110 * it has finished using the exported string.
112 extern char *mesh_event_sock_create(const char *ifname);
114 /// Waits for the mesh event for about the given timeout.
115 /** This function waits for the mesh event that's expected to occur for the given timeout. If a mesh event
116 * is received then the given callback will be invoked.
118 * @param callback callback which handles the mesh event packet.
119 * @param timeout timeout for which the the function has to wait for the event.
121 * @return This function returns true if a mesh event occurred else false if timeout exceeded.
123 extern bool wait_for_event(mesh_event_callback_t callback, int timeout);
125 /// Sends the mesh event to server.
126 /** This function sends the mesh event to the server. At the server end it's expected to wait_for_event()
127 * otherwise the packet will be dropped.
129 * @param client_id Client id by which server can identify the client/node.
130 * @param event An enum describing the mesh event.
131 * @param payload Payload can also be attached along with the mesh event if any, else NULL can
133 * @param payload_length Length of the payload if specified else 0 can be specified.
134 * the maximum payload size can be up to PAYLOAD_MAX_SIZE and if the
135 * PAYLOAD_MAX_SIZE macro is changed it should not exceed the UDP datagram size.
137 * @return This function returns true on success else returns false.
139 extern bool mesh_event_sock_send(int client_id, mesh_event_t event, const void *payload, size_t payload_length);
141 /// Imports the server address, saves it and opens an UDP client socket.
142 /** This function creates an UDP socket, binds it with given interface address and returns a NULL
143 * terminated string containing server's IP address & port number.
145 * @param server_address NULL terminated string that's exported by mesh_event_sock_create() which
146 * which contains IP address and port number of the mesh event server.
150 extern void mesh_event_sock_connect(const char *server_address);
152 extern void mesh_event_destroy(void);
154 extern void mesh_events_flush(void);
156 #endif // _MESH_EVENT_HANDLER_H_