]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/mesh_event_handler.h
4300affb39cdbd57f54fb0dfbd54b70b1cdb17ad
[meshlink] / test / blackbox / common / mesh_event_handler.h
1 /*
2     mesh_event_handler.h
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef _MESH_EVENT_HANDLER_H_
21 #define _MESH_EVENT_HANDLER_H_
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <sys/types.h>
29 #include <net/if.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <stdbool.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <fcntl.h>
36 #include <time.h>
37 #include <stdint.h>
38
39 /// Maximum length of the mesh event payload
40 #define PAYLOAD_MAX_SIZE 1000
41
42 /// mesh events
43 // TODO: Add more mesh event if required.
44 typedef enum {
45         NO_PREFERENCE = 0,
46         META_CONN_SUCCESSFUL,
47         META_CONN,
48         META_DISCONN,
49         META_CONN_CLOSED,
50         NODE_INVITATION,
51         CHANGED_IP_ADDRESS,
52         NODE_UNREACHABLE,
53         NODE_REACHABLE,
54         META_RECONN_SUCCESSFUL,
55         META_RECONN_FAILURE,
56         MESH_DATA_RECEIVED,
57         NODE_STARTED,
58         NODE_RESTARTED,
59         NODE_JOINED,
60         PORT_NO,
61         ERR_NETWORK,
62         MESH_DATA_VERIFED,
63         CHANNEL_OPENED,
64         CHANNEL_REQ_RECIEVED,
65         CHANNEL_CONNECTED,
66         CHANNEL_DATA_RECIEVED,
67         MESH_NODE_DISCOVERED,
68         INCOMING_META_CONN,
69         OUTGOING_META_CONN,
70         AUTO_DISCONN,
71
72         MAX_EVENT           // Maximum event enum
73 } mesh_event_t;
74
75 /// mesh event UDP packet
76 typedef struct mesh_event_payload {
77         uint32_t      client_id;
78         mesh_event_t  mesh_event;
79         uint16_t      payload_length;
80         uint8_t       payload[PAYLOAD_MAX_SIZE];
81 } mesh_event_payload_t;
82
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.
85  *
86  *  @param mesh_event_packet    packet containing client-id, mesh event & payload (if any).
87  */
88 typedef void (*mesh_event_callback_t)(mesh_event_payload_t mesh_event_packet);
89
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.
93  *
94  *  @param ifname       Name of the network interface to which the socket has to be created.
95  *
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.
99  */
100 extern char *mesh_event_sock_create(const char *ifname);
101
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.
105  *
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.
108  *
109  *  @return             This function returns true if a mesh event occured else false if timeout exceeded.
110  */
111 extern bool wait_for_event(mesh_event_callback_t callback, int timeout);
112
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.
116  *
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
120  *                          can be specified.
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.
124  *
125  *  @return                  This function returns true on success else returns false.
126  */
127 extern bool mesh_event_sock_send(int client_id, mesh_event_t event, void *payload, size_t payload_length);
128
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.
132  *
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.
135  *
136  *  @return                  void
137  */
138 extern void mesh_event_sock_connect(const char *server_address);
139
140 bool wait_for_event_only(mesh_event_callback_t callback, int t, mesh_event_t event);
141 #endif // _MESH_EVENT_HANDLER_H_