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>
39 /// Maximum length of the mesh event payload
40 #define PAYLOAD_MAX_SIZE 1000
43 // TODO: Add more mesh event if required.
54 META_RECONN_SUCCESSFUL,
66 CHANNEL_DATA_RECIEVED,
72 MAX_EVENT // Maximum event enum
75 /// mesh event UDP packet
76 typedef struct mesh_event_payload {
78 mesh_event_t mesh_event;
79 uint16_t payload_length;
80 uint8_t payload[PAYLOAD_MAX_SIZE];
81 } mesh_event_payload_t;
83 /// callback for handling the mesh event
84 /** mesh event callback called from wait_for_event() if the mesh event UDP server gets a mesh event.
86 * @param mesh_event_packet packet containing client-id, mesh event & payload (if any).
88 typedef void (*mesh_event_callback_t)(mesh_event_payload_t mesh_event_packet);
90 /// Creates an UDP server for listening mesh events.
91 /** This function creates an UDP socket, binds it with given interface address and returns a NULL
92 * terminated string containing server's IP address & port number.
94 * @param ifname Name of the network interface to which the socket has to be created.
96 * @return This function returns a NULL terminated string which has IP address and
97 * port number of the server socket. The application should call free() after
98 * it has finished using the exported string.
100 extern char *mesh_event_sock_create(const char *ifname);
102 /// Waits for the mesh event for about the given timeout.
103 /** This function waits for the mesh event that's expected to occur for the given timeout. If a mesh event
104 * is received then the given callback will be invoked.
106 * @param callback callback which handles the mesh event packet.
107 * @param timeout timeout for which the the function has to wait for the event.
109 * @return This function returns true if a mesh event occured else false if timeout exceeded.
111 extern bool wait_for_event(mesh_event_callback_t callback, int timeout);
113 /// Sends the mesh event to server.
114 /** This function sends the mesh event to the server. At the server end it's expected to wait_for_event()
115 * otherwise the packet will be dropped.
117 * @param client_id Client id by which server can identify the client/node.
118 * @param event An enum describing the mesh event.
119 * @param payload Payload can also be attached along with the mesh event if any, else NULL can
121 * @param payload_length Length of the payload if specified else 0 can be specified.
122 * the maximum payload size can be upto PAYLOAD_MAX_SIZE and if the
123 * PAYLOAD_MAX_SIZE macro is changed it should not exceed the UDP datagram size.
125 * @return This function returns true on success else returns false.
127 extern bool mesh_event_sock_send(int client_id, mesh_event_t event, void *payload, size_t payload_length);
129 /// Imports the server address, saves it and opens an UDP client socket.
130 /** This function creates an UDP socket, binds it with given interface address and returns a NULL
131 * terminated string containing server's IP address & port number.
133 * @param server_address NULL terminated string that's exported by mesh_event_sock_create() which
134 * which contains IP address and port number of the mesh event server.
138 extern void mesh_event_sock_connect(const char *server_address);
140 bool wait_for_event_only(mesh_event_callback_t callback, int t, mesh_event_t event);
141 #endif // _MESH_EVENT_HANDLER_H_