]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/mesh_event_handler.h
Add blackbox test cases for submesh
[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 /// mesh events
40 // TODO: Add more mesh event if required.
41 typedef enum {
42         NO_PREFERENCE = 0,
43         META_CONN_SUCCESSFUL,
44         META_CONN,
45         META_DISCONN,
46         META_CONN_CLOSED,
47         NODE_INVITATION,
48         CHANGED_IP_ADDRESS,
49         NODE_UNREACHABLE,
50         NODE_REACHABLE,
51         META_RECONN_SUCCESSFUL,
52         META_RECONN_FAILURE,
53         MESH_DATA_RECEIVED,
54         NODE_STARTED,
55         NODE_LEFT,
56         NODE_RESTARTED,
57         NODE_JOINED,
58         NODE_JOINED1,
59         NODE_JOINED2,
60         NODE_JOINED3,
61         PORT_NO,
62         OPTIMAL_PMTU_PEER,
63         OPTIMAL_PMTU_RELAY,
64         ERR_NETWORK,
65         SIG_ABORT,
66         MESH_DATA_VERIFED,
67         CHANNEL_OPENED,
68         CHANNEL_REQ_RECIEVED,
69         CHANNEL_CONNECTED,
70         CHANNEL_DATA_RECIEVED,
71         MESH_NODE_DISCOVERED,
72         INCOMING_META_CONN,
73         OUTGOING_META_CONN,
74         AUTO_DISCONN,
75         MESH_EVENT_COMPLETED,
76         MAX_EVENT           // Maximum event enum
77 } mesh_event_t;
78
79 extern const char *event_status[];
80
81 /// mesh event UDP packet
82 typedef struct  mesh_event_payload {
83         void          *payload;
84         mesh_event_t  mesh_event;
85         uint16_t      client_id;
86         uint32_t       payload_length;
87 } mesh_event_payload_t;
88
89 struct cond_flag {
90         pthread_mutex_t mutex;
91         pthread_cond_t cond;
92         bool flag;
93 };
94
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.
97  *
98  *  @param mesh_event_packet    packet containing client-id, mesh event & payload (if any).
99  */
100 typedef bool (*mesh_event_callback_t)(mesh_event_payload_t mesh_event_packet);
101
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.
105  *
106  *  @param ifname       Name of the network interface to which the socket has to be created.
107  *
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.
111  */
112 extern char *mesh_event_sock_create(const char *ifname);
113
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.
117  *
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.
120  *
121  *  @return             This function returns true if a mesh event occured else false if timeout exceeded.
122  */
123 extern bool wait_for_event(mesh_event_callback_t callback, int timeout);
124
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.
128  *
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
132  *                          can be specified.
133  *  @param payload_length   Length of the payload if specified else 0 can be specified.
134  *                                                                                                      the maximum payload size can be upto PAYLOAD_MAX_SIZE and if the
135  *                          PAYLOAD_MAX_SIZE macro is changed it should not exceed the UDP datagram size.
136  *
137  *  @return                  This function returns true on success else returns false.
138  */
139 extern bool mesh_event_sock_send(int client_id, mesh_event_t event, const void *payload, size_t payload_length);
140
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.
144  *
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.
147  *
148  *  @return                  void
149  */
150 extern void mesh_event_sock_connect(const char *server_address);
151
152 extern void mesh_event_destroy(void);
153
154 extern void mesh_events_flush(void);
155
156 #endif // _MESH_EVENT_HANDLER_H_