9 #ifndef IP_CONNECTION_H 10 #define IP_CONNECTION_H 16 #ifndef __STDC_LIMIT_MACROS 17 #define __STDC_LIMIT_MACROS 23 #if !defined __cplusplus && defined __GNUC__ 28 #ifndef WIN32_LEAN_AND_MEAN 29 #define WIN32_LEAN_AND_MEAN 34 #include <semaphore.h> 40 E_NO_STREAM_SOCKET = -2,
41 E_HOSTNAME_INVALID = -3,
45 E_ALREADY_CONNECTED = -7,
47 E_INVALID_PARAMETER = -9,
48 E_NOT_SUPPORTED = -10,
49 E_UNKNOWN_ERROR_CODE = -11
52 #ifdef IPCON_EXPOSE_INTERNALS 58 CRITICAL_SECTION handle;
60 pthread_mutex_t handle;
64 void mutex_create(Mutex *mutex);
66 void mutex_destroy(Mutex *mutex);
68 void mutex_lock(Mutex *mutex);
70 void mutex_unlock(Mutex *mutex);
76 pthread_cond_t condition;
77 pthread_mutex_t mutex;
91 typedef void (*ThreadFunction)(
void *opaque);
100 ThreadFunction
function;
112 typedef struct _QueueItem {
113 struct _QueueItem *next;
126 #if defined _MSC_VER || defined __BORLANDC__ 129 #define ATTRIBUTE_PACKED 130 #elif defined __GNUC__ 134 #define ATTRIBUTE_PACKED __attribute__((gcc_struct, packed)) 136 #define ATTRIBUTE_PACKED __attribute__((packed)) 139 #error unknown compiler, do not know how to enable struct packing 146 uint8_t sequence_number_and_options;
147 uint8_t error_code_and_future_use;
153 uint8_t optional_data[8];
156 #if defined _MSC_VER || defined __BORLANDC__ 159 #undef ATTRIBUTE_PACKED 161 #endif // IPCON_EXPOSE_INTERNALS 164 typedef struct _IPConnectionPrivate IPConnectionPrivate;
166 typedef struct _DevicePrivate DevicePrivate;
168 #ifdef IPCON_EXPOSE_INTERNALS 174 typedef void (*EnumerateCallbackFunction)(
const char *uid,
175 const char *connected_uid,
177 uint8_t hardware_version[3],
178 uint8_t firmware_version[3],
179 uint16_t device_identifier,
180 uint8_t enumeration_type,
182 typedef void (*ConnectedCallbackFunction)(uint8_t connect_reason,
184 typedef void (*DisconnectedCallbackFunction)(uint8_t disconnect_reason,
187 #ifdef IPCON_EXPOSE_INTERNALS 189 typedef void (*CallbackWrapperFunction)(DevicePrivate *device_p, Packet *packet);
200 #ifdef IPCON_EXPOSE_INTERNALS 202 #define DEVICE_NUM_FUNCTION_IDS 256 207 struct _DevicePrivate {
210 IPConnectionPrivate *ipcon_p;
212 uint8_t api_version[3];
216 uint8_t expected_response_function_id;
217 uint8_t expected_response_sequence_number;
218 Mutex response_mutex;
219 Packet response_packet;
220 Event response_event;
221 int response_expected[DEVICE_NUM_FUNCTION_IDS];
223 void *registered_callbacks[DEVICE_NUM_FUNCTION_IDS];
224 void *registered_callback_user_data[DEVICE_NUM_FUNCTION_IDS];
225 CallbackWrapperFunction callback_wrappers[DEVICE_NUM_FUNCTION_IDS];
232 DEVICE_RESPONSE_EXPECTED_INVALID_FUNCTION_ID = 0,
233 DEVICE_RESPONSE_EXPECTED_ALWAYS_TRUE,
234 DEVICE_RESPONSE_EXPECTED_ALWAYS_FALSE,
235 DEVICE_RESPONSE_EXPECTED_TRUE,
236 DEVICE_RESPONSE_EXPECTED_FALSE
242 void device_create(
Device *device,
const char *uid,
243 IPConnectionPrivate *ipcon_p, uint8_t api_version_major,
244 uint8_t api_version_minor, uint8_t api_version_release);
249 void device_destroy(
Device *device);
254 int device_get_response_expected(DevicePrivate *device_p, uint8_t function_id,
255 bool *ret_response_expected);
260 int device_set_response_expected(DevicePrivate *device_p, uint8_t function_id,
261 bool response_expected);
266 int device_set_response_expected_all(DevicePrivate *device_p,
bool response_expected);
271 void device_register_callback(DevicePrivate *device_p, uint8_t
id,
void *callback,
277 int device_get_api_version(DevicePrivate *device_p, uint8_t ret_api_version[3]);
282 int device_send_request(DevicePrivate *device_p, Packet *request, Packet *response);
284 #endif // IPCON_EXPOSE_INTERNALS 292 IPCON_CALLBACK_ENUMERATE = 253,
293 IPCON_CALLBACK_CONNECTED = 0,
294 IPCON_CALLBACK_DISCONNECTED = 1
303 IPCON_ENUMERATION_TYPE_AVAILABLE = 0,
304 IPCON_ENUMERATION_TYPE_CONNECTED = 1,
305 IPCON_ENUMERATION_TYPE_DISCONNECTED = 2
314 IPCON_CONNECT_REASON_REQUEST = 0,
315 IPCON_CONNECT_REASON_AUTO_RECONNECT = 1
324 IPCON_DISCONNECT_REASON_REQUEST = 0,
325 IPCON_DISCONNECT_REASON_ERROR = 1,
326 IPCON_DISCONNECT_REASON_SHUTDOWN = 2
335 IPCON_CONNECTION_STATE_DISCONNECTED = 0,
336 IPCON_CONNECTION_STATE_CONNECTED = 1,
337 IPCON_CONNECTION_STATE_PENDING = 2
344 IPConnectionPrivate *p;
347 #ifdef IPCON_EXPOSE_INTERNALS 349 #define IPCON_NUM_CALLBACK_IDS 256 354 struct _IPConnectionPrivate {
356 bool wsa_startup_done;
365 bool auto_reconnect_allowed;
366 bool auto_reconnect_pending;
368 Mutex sequence_number_mutex;
369 uint8_t next_sequence_number;
373 void *registered_callbacks[IPCON_NUM_CALLBACK_IDS];
374 void *registered_callback_user_data[IPCON_NUM_CALLBACK_IDS];
381 Thread receive_thread;
383 CallbackContext *callback;
385 bool disconnect_probe_flag;
386 Thread disconnect_probe_thread;
387 Event disconnect_probe_event;
392 #endif // IPCON_EXPOSE_INTERNALS 520 void *callback,
void *user_data);
522 #ifdef IPCON_EXPOSE_INTERNALS 527 int packet_header_create(PacketHeader *header, uint8_t length,
528 uint8_t function_id, IPConnectionPrivate *ipcon_p,
529 DevicePrivate *device_p);
534 uint8_t packet_header_get_sequence_number(PacketHeader *header);
539 void packet_header_set_sequence_number(PacketHeader *header,
540 uint8_t sequence_number);
545 uint8_t packet_header_get_response_expected(PacketHeader *header);
550 void packet_header_set_response_expected(PacketHeader *header,
551 uint8_t response_expected);
556 uint8_t packet_header_get_error_code(PacketHeader *header);
561 int16_t leconvert_int16_to(int16_t native);
566 uint16_t leconvert_uint16_to(uint16_t native);
571 int32_t leconvert_int32_to(int32_t native);
576 uint32_t leconvert_uint32_to(uint32_t native);
581 int64_t leconvert_int64_to(int64_t native);
586 uint64_t leconvert_uint64_to(uint64_t native);
591 float leconvert_float_to(
float native);
596 int16_t leconvert_int16_from(int16_t little);
601 uint16_t leconvert_uint16_from(uint16_t little);
606 int32_t leconvert_int32_from(int32_t little);
611 uint32_t leconvert_uint32_from(uint32_t little);
616 int64_t leconvert_int64_from(int64_t little);
621 uint64_t leconvert_uint64_from(uint64_t little);
626 float leconvert_float_from(
float little);
628 #endif // IPCON_EXPOSE_INTERNALS Definition: ip_connection.c:169
Definition: ip_connection.h:343
void ipcon_destroy(IPConnection *ipcon)
Destroys the IP Connection object.
Definition: ip_connection.c:1608
int ipcon_enumerate(IPConnection *ipcon)
Broadcasts an enumerate request.
Definition: ip_connection.c:1749
Definition: bricklet_led_strip.c:38
void ipcon_set_timeout(IPConnection *ipcon, uint32_t timeout)
Sets the timeout in milliseconds for getters and for setters for which the response expected flag is ...
Definition: ip_connection.c:1741
Definition: ip_connection.c:1010
Definition: ip_connection.h:196
void ipcon_create(IPConnection *ipcon)
Creates an IP Connection object that can be used to enumerate the available devices.
Definition: ip_connection.c:1564
void ipcon_unwait(IPConnection *ipcon)
Unwaits the thread previously stopped by ipcon_wait.
Definition: ip_connection.c:1768
int ipcon_disconnect(IPConnection *ipcon)
Disconnects the TCP/IP connection from the Brick Daemon or the WIFI/Ethernet Extension.
Definition: ip_connection.c:1667
bool ipcon_get_auto_reconnect(IPConnection *ipcon)
Returns true if auto-reconnect is enabled, false otherwise.
Definition: ip_connection.c:1737
uint32_t ipcon_get_timeout(IPConnection *ipcon)
Returns the timeout as set by ipcon_set_timeout.
Definition: ip_connection.c:1745
int ipcon_connect(IPConnection *ipcon, const char *host, uint16_t port)
Creates a TCP/IP connection to the given host and c\ port.
Definition: ip_connection.c:1628
int ipcon_get_connection_state(IPConnection *ipcon)
Can return the following states:
Definition: ip_connection.c:1714
void ipcon_register_callback(IPConnection *ipcon, uint8_t id, void *callback, void *user_data)
Registers a callback for a given ID.
Definition: ip_connection.c:1772
void ipcon_wait(IPConnection *ipcon)
Stops the current thread until ipcon_unwait is called.
Definition: ip_connection.c:1764
void ipcon_set_auto_reconnect(IPConnection *ipcon, bool auto_reconnect)
Enables or disables auto-reconnect.
Definition: ip_connection.c:1726