Initial import of NFC GNSS SDK project

This commit is contained in:
chenjintao
2026-09-15 16:10:12 +08:00
commit 2746cc3d4e
3070 changed files with 892062 additions and 0 deletions
@@ -0,0 +1,388 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __LIBRWS_H__
#define __LIBRWS_H__
#include <stdio.h>
#define RWS_VERSION_MAJOR 1
#define RWS_VERSION_MINOR 2
#define RWS_VERSION_PATCH 4
// check windows
#if defined(WIN32) || defined(_WIN32) || defined(WIN32_LEAN_AND_MEAN) || defined(_WIN64) || defined(WIN64)
#define RWS_OS_WINDOWS 1
#endif
// extern
#if defined(__cplusplus) || defined(_cplusplus)
#define RWS_EXTERN extern "C"
#else
#define RWS_EXTERN extern
#endif
// attribute
#if defined(__GNUC__)
#if (__GNUC__ >= 4)
#if defined(__cplusplus) || defined(_cplusplus)
#define RWS_ATTRIB __attribute__((visibility("default")))
#else
#define RWS_ATTRIB __attribute__((visibility("default")))
#endif
#endif
#endif
// check attrib and define empty if not defined
#if !defined(RWS_ATTRIB)
#define RWS_ATTRIB
#endif
// dll api
#if defined(RWS_OS_WINDOWS)
#if defined(RWS_BUILD)
#define RWS_DYLIB_API __declspec(dllexport)
#else
#define RWS_DYLIB_API __declspec(dllimport)
#endif
#endif
// check dll api and define empty if not defined
#if !defined(RWS_DYLIB_API)
#define RWS_DYLIB_API
#endif
// combined lib api
#define RWS_API(return_type) RWS_EXTERN RWS_ATTRIB RWS_DYLIB_API return_type
// types
/**
@brief Boolean type as unsigned byte type.
*/
typedef unsigned char rws_bool;
#define rws_true 1
#define rws_false 0
/**
@brief Type of all public objects.
*/
typedef void *rws_handle;
/**
@brief Socket handle.
*/
typedef rws_handle rws_socket;
/**
@brief Error object handle.
*/
typedef rws_handle rws_error;
/**
@brief Mutex object handle.
*/
typedef rws_handle rws_mutex;
/**
@brief Thread object handle.
*/
typedef rws_handle rws_thread;
/**
@brief Callback type of thread function.
@param user_object User object provided during thread creation.
*/
typedef void (*rws_thread_funct)(void *user_object);
/**
@brief Callback type of socket object.
@param socket Socket object.
*/
typedef void (*rws_on_socket)(rws_socket socket);
/**
@brief Callback type on socket receive text frame.
@param socket Socket object.
@param text Pointer to reseived text.
@param length Received text lenght without null terminated char.
*/
typedef void (*rws_on_socket_recvd_text)(rws_socket socket, const char *text, const unsigned int length);
/**
@brief Callback type on socket receive binary frame.
@param socket Socket object.
@param data Received binary data.
@param length Received binary data lenght.
*/
typedef void (*rws_on_socket_recvd_bin)(rws_socket socket, const void *data, const unsigned int length);
// socket
/**
@brief Create new socket.
@return Socket handler or NULL on error.
*/
RWS_API(rws_socket) rws_socket_create(void);
/**
@brief Set socket connect URL.
@param socket Socket object.
@param scheme Connect URL scheme, "http" or "ws"
@param scheme Connect URL host, "echo.websocket.org"
@param scheme Connect URL port.
@param scheme Connect URL path started with '/' character, "/" - for empty, "/path"
@code
rws_socket_set_url(socket, "http", "echo.websocket.org", 80, "/");
rws_socket_set_url(socket, "ws", "echo.websocket.org", 80, "/");
@endcode
*/
RWS_API(void) rws_socket_set_url(rws_socket socket,
const char *scheme,
const char *host,
const int port,
const char *path);
/**
@brief Set socket connect URL scheme string.
@param socket Socket object.
@param scheme Connect URL scheme, "http" or "ws"
@code
rws_socket_set_scheme(socket, "http");
rws_socket_set_scheme(socket, "ws");
@endcode
*/
RWS_API(void) rws_socket_set_scheme(rws_socket socket, const char *scheme);
/**
@brief Get socket connect URL scheme string.
@param socket Socket object.
@return Connect URL cheme or null.
*/
RWS_API(const char *) rws_socket_get_scheme(rws_socket socket);
/**
@brief Set socket connect URL scheme string.
@param socket Socket object.
@param scheme Connect URL host, "echo.websocket.org"
@code
rws_socket_set_host(socket, "echo.websocket.org");
@endcode
*/
RWS_API(void) rws_socket_set_host(rws_socket socket, const char *host);
/**
@brief Get socket connect URL host string.
@param socket Socket object.
@return Connect URL host or null.
*/
RWS_API(const char *) rws_socket_get_host(rws_socket socket);
/**
@brief Set socket connect URL port.
@param socket Socket object.
@param scheme Connect URL port.
@code
rws_socket_set_port(socket, 80);
@endcode
*/
RWS_API(void) rws_socket_set_port(rws_socket socket, const int port);
/**
@brief Get socket connect URL port.
@param socket Socket object.
@return Connect URL port or 0.
*/
RWS_API(int) rws_socket_get_port(rws_socket socket);
/**
@brief Set socket connect URL path string.
@param socket Socket object.
@param scheme Connect URL path started with '/' character, "/" - for empty, "/path"
@code
rws_socket_set_path(socket, "/"); // empty path
rws_socket_set_path(socket, "/path"); // some path
@endcode
*/
RWS_API(void) rws_socket_set_path(rws_socket socket, const char *path);
/**
@brief Get socket connect URL path string.
@param socket Socket object.
@return Connect URL path or null.
*/
RWS_API(const char *) rws_socket_get_path(rws_socket socket);
/**
@brief Get socket last error object handle.
@param socket Socket object.
@return Last error object handle or null if no error.
*/
RWS_API(rws_error) rws_socket_get_error(rws_socket socket);
/**
@brief Start connection.
@detailed This method can generate error object.
@param socket Socket object.
@return rws_true - all params exists and connection started, otherwice rws_false.
*/
RWS_API(rws_bool) rws_socket_connect(rws_socket socket);
/**
@brief Disconnect socket.
@detailed Cleanup prev. send messages and start disconnection sequence.
SHOULD forget about this socket handle and don't use it anymore.
@warning Don't use this socket object handler after this command.
@param socket Socket object.
*/
RWS_API(void) rws_socket_disconnect_and_release(rws_socket socket);
/**
@brief Check is socket has connection to host and handshake(sucessfully done).
@detailed Thread safe getter.
@param socket Socket object.
@return trw_true - connected to host and handshacked, otherwice rws_false.
*/
RWS_API(rws_bool) rws_socket_is_connected(rws_socket socket);
/**
@brief Send text to connect socket.
@detailed Thread safe method.
@param socket Socket object.
@param text Text string for sending.
@return rws_true - socket and text exists and placed to send queue, otherwice rws_false.
*/
RWS_API(rws_bool) rws_socket_send_text(rws_socket socket, const char *text);
/**
@brief Send bin to connect socket.
@detailed Thread safe method.
@param socket Socket object.
@param bin bin string for sending.
@param opcode opcode for frame
@return rws_true - socket and bin exists and placed to send queue, otherwice rws_false.
*/
RWS_API(rws_bool) rws_socket_send_bin(rws_socket socket, void *data, size_t len, int opcode, rws_bool is_fin);
/**
@brief Set socket user defined object pointer for identificating socket object.
@param socket Socket object.
@param user_object Void pointer to user object.
*/
RWS_API(void) rws_socket_set_user_object(rws_socket socket, void *user_object);
/**
@brief Get socket user defined object.
@param socket Socket object.
@return User defined object pointer or null.
*/
RWS_API(void *) rws_socket_get_user_object(rws_socket socket);
RWS_API(void) rws_socket_set_on_connected(rws_socket socket, rws_on_socket callback);
RWS_API(void) rws_socket_set_on_disconnected(rws_socket socket, rws_on_socket callback);
RWS_API(void) rws_socket_set_on_received_text(rws_socket socket, rws_on_socket_recvd_text callback);
RWS_API(void) rws_socket_set_on_received_bin(rws_socket socket, rws_on_socket_recvd_bin callback);
#ifdef LIBRWS_USING_MBED_TLS
RWS_API(void) rws_socket_set_server_cert(rws_socket socket, const char *server_cert, int server_cert_len);
#endif
// error
typedef enum _rws_error_code
{
rws_error_code_none = 0,
rws_error_code_missed_parameter,
rws_error_code_send_handshake,
rws_error_code_parse_handshake,
rws_error_code_read_write_socket,
rws_error_code_connect_to_host,
/**
@brief Connection was closed by endpoint.
Reasons: an endpoint shutting down, an endpoint having received a frame too large, or an
endpoint having received a frame that does not conform to the format expected by the endpoint.
*/
rws_error_code_connection_closed,
} rws_error_code;
/**
@return 0 - if error is empty or no error, otherwice error code.
*/
RWS_API(int) rws_error_get_code(rws_error error);
/**
@return 0 - if error is empty or no error, otherwice HTTP error.
*/
RWS_API(int) rws_error_get_http_error(rws_error error);
/**
@brief Get description of the error object.
*/
RWS_API(const char *) rws_error_get_description(rws_error error);
// mutex
/**
@brief Creates recursive mutex object.
*/
RWS_API(rws_mutex) rws_mutex_create_recursive(void);
/**
@brief Lock mutex object.
*/
RWS_API(void) rws_mutex_lock(rws_mutex mutex);
/**
@brief Unlock mutex object.
*/
RWS_API(void) rws_mutex_unlock(rws_mutex mutex);
/**
@brief Unlock mutex object.
*/
RWS_API(void) rws_mutex_delete(rws_mutex mutex);
// thread
/**
@brief Create thread object that start immidiatelly.
*/
RWS_API(rws_thread) rws_thread_create(rws_thread_funct thread_function, void *user_object);
/**
@brief Pause current thread for a number of milliseconds.
*/
RWS_API(void) rws_thread_sleep(const unsigned int millisec);
/* RT-Thread Team add */
RWS_API(void) rws_socket_set_custom_mode(rws_socket socket);
#define RWS_RECEIVE_HEADER_BUFF_SIZE ( 14)
#define RWS_RECEIVE_PAYLOAD_BUFF_SIZE (2048)
#endif
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_COMMON_H__
#define __RWS_COMMON_H__ 1
#include <stdio.h>
#include "ql_type.h"
#include "ql_rtos.h"
#define DBG_SECTION_NAME "[LIBRWS] "
#define DBG_ENABLE
#define DBG_LEVEL DBG_WARNING
#define DBG_COLOR
#define DBG_FUNCTION_NUM 20
#define LOG_W printf
#define LOG_I printf
#define LOG_E printf
#define LOG_D printf
/* check os */
/* gcc -dM -E - < /dev/null */
/* check windows */
#if !defined(RWS_OS_WINDOWS)
#if defined(WIN32) || defined(_WIN32) || defined(WIN32_LEAN_AND_MEAN) || defined(_WIN64) || defined(WIN64)
#define RWS_OS_WINDOWS 1
#endif
#endif
/* end check windows */
/* check Apple */
#if !defined(RWS_OS_APPLE)
#if defined(__APPLE__) || defined(__MACH__)
#define RWS_OS_APPLE 1
#endif
#endif
/* check Unix */
#if !defined(RWS_OS_UNIX)
#if defined(__unix) || defined(unix) || defined(__unix__)
#define RWS_OS_UNIX 1
#endif
#endif
/* check Linux */
#if !defined(RWS_OS_LINUX)
#if defined(__linux__) || defined(__linux)
#define RWS_OS_LINUX 1
#endif
#endif
/* check Android */
#if !defined(RWS_OS_ANDROID)
#if defined(__ANDROID__) || defined(ANDROID)
#define RWS_OS_ANDROID 1
#endif
#endif
#endif
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_ERROR_H__
#define __RWS_ERROR_H__ 1
#include "rws_string.h"
typedef struct _rws_error_struct
{
int code;
int http_error;
char *description;
} _rws_error;
_rws_error *rws_error_create(void);
_rws_error *rws_error_new_code_descr(const int code, const char *description);
void rws_error_delete(_rws_error *error);
void rws_error_delete_clean(_rws_error **error);
#endif
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_FRAME_H__
#define __RWS_FRAME_H__ 1
#include "librws.h"
#include "rws_common.h"
typedef enum _rws_opcode
{
rws_opcode_continuation = 0x0, // %x0 denotes a continuation frame
rws_opcode_text_frame = 0x1, // %x1 denotes a text frame
rws_opcode_binary_frame = 0x2, // %x2 denotes a binary frame
rws_opcode_connection_close = 0x8, // %x8 denotes a connection close
rws_opcode_ping = 0x9, // %x9 denotes a ping
rws_opcode_pong = 0xA // %xA denotes a pong
} rws_opcode;
typedef struct _rws_frame_struct
{
void *data;
size_t data_size;
rws_opcode opcode;
unsigned char mask[4];
rws_bool is_masked;
rws_bool is_finished;
unsigned char header_size;
} _rws_frame;
size_t rws_check_recv_frame_size(const void *data, const size_t data_size);
_rws_frame *rws_frame_create_with_recv_data(const void *data, const size_t data_size);
// data - should be null, and setted by newly created. 'data' & 'data_size' can be null
void rws_frame_fill_with_send_data(_rws_frame *f, const void *data, const size_t data_size);
// combine datas of 2 frames. combined is 'to' /
void rws_frame_combine_datas(_rws_frame *to, _rws_frame *from);
_rws_frame *rws_frame_create(void);
void rws_frame_delete(_rws_frame *f);
void rws_frame_delete_clean(_rws_frame **f);
/* RT-Thread Team add */
size_t rws_frame_get_header_size(const char *data, const size_t len);
size_t rws_frame_get_payload_len(const char *data, const size_t len);
rws_opcode rws_frame_get_opcode(const char *data, const size_t len);
void rws_frame_fill_with_send_bin(_rws_frame *f, const void *data, const size_t data_size, rws_bool is_fin);
#endif
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_LIST_H__
#define __RWS_LIST_H__ 1
#include <stdio.h>
typedef union _rws_node_value_union
{
void *object;
char *string;
int int_value;
unsigned int uint_value;
} _rws_node_value;
typedef struct _rws_node_struct
{
_rws_node_value value;
struct _rws_node_struct *next;
} _rws_node, _rws_list;
_rws_list *rws_list_create(void);
void rws_list_delete(_rws_list *list);
void rws_list_delete_clean(_rws_list **list);
void rws_list_append(_rws_list *list, _rws_node_value value);
#endif
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_MEMORY_H__
#define __RWS_MEMORY_H__ 1
#include <stdio.h>
#include <stdlib.h>
#include "rws_common.h"
// size > 0 => malloc
void *rws_malloc(const size_t size);
// size > 0 => malloc
void *rws_malloc_zero(const size_t size);
void rws_free(void *mem);
void rws_free_clean(void **mem);
#endif
@@ -0,0 +1,213 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_SOCKET_H__
#define __RWS_SOCKET_H__ 1
#include "rws_common.h"
#if defined(RWS_OS_WINDOWS)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#include "sockets.h"
#include <sys/types.h>
#include <unistd.h>
#endif
#include <assert.h>
#include <errno.h>
#include "rws_error.h"
#include "rws_thread.h"
#include "rws_frame.h"
#include "rws_list.h"
#ifdef LIBRWS_USING_MBED_TLS
#include "mbedtls/net.h"
#include "mbedtls/ssl.h"
#include "mbedtls/certs.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/debug.h"
#endif
#if defined(RWS_OS_WINDOWS)
typedef SOCKET rws_socket_t;
#define RWS_INVALID_SOCKET INVALID_SOCKET
#define RWS_SOCK_CLOSE(sock) closesocket(sock)
#else
typedef int rws_socket_t;
#define RWS_INVALID_SOCKET -1
#define RWS_SOCK_CLOSE(sock) close(sock)
#endif
#ifdef LIBRWS_USING_MBED_TLS
typedef struct _rws_ssl_struct
{
mbedtls_ssl_context ssl_ctx; /* mbedtls ssl context */
mbedtls_net_context net_ctx; /* Fill in socket id */
mbedtls_ssl_config ssl_conf; /* SSL configuration */
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_x509_crt_profile profile;
mbedtls_x509_crt cacert;
mbedtls_x509_crt clicert;
mbedtls_pk_context pkey;
} _rws_ssl;
#endif
typedef struct _rws_socket_struct
{
int port;
rws_socket_t socket;
char *scheme;
char *host;
char *path;
char *sec_ws_accept; // "Sec-WebSocket-Accept" field from handshake
rws_thread work_thread;
int command;
unsigned int next_message_id;
rws_bool is_connected; // sock connected + handshake done
void *user_object;
rws_on_socket on_connected;
rws_on_socket on_disconnected;
rws_on_socket_recvd_text on_recvd_text;
rws_on_socket_recvd_bin on_recvd_bin;
void *received;
size_t received_size; // size of 'received' memory
size_t received_len; // length of actualy readed message
_rws_list *send_frames;
_rws_list *recvd_frames;
_rws_error *error;
rws_mutex work_mutex;
rws_mutex send_mutex;
int custom_mode;
char *header_buff;
char *payload_buff;
#ifdef LIBRWS_USING_MBED_TLS
const char *server_cert; /**< Server certification. */
const char *client_cert; /**< Client certification. */
const char *client_pk; /**< Client private key. */
int server_cert_len; /**< Server certification lenght, server_cert buffer size. */
int client_cert_len; /**< Client certification lenght, client_cert buffer size. */
int client_pk_len; /**< Client private key lenght, client_pk buffer size. */
_rws_ssl *ssl; /**< SSL content. */
#endif
} _rws_socket;
rws_bool rws_socket_process_handshake_responce(_rws_socket *s);
// receive raw data from socket
rws_bool rws_socket_recv(_rws_socket *s);
/* RT-Thread Team add */
rws_bool rws_socket_recv_custom(_rws_socket *s);
// send raw data to socket
rws_bool rws_socket_send(_rws_socket *s, const void *data, const size_t data_size);
_rws_frame *rws_socket_last_unfin_recvd_frame_by_opcode(_rws_socket *s, const rws_opcode opcode);
void rws_socket_process_bin_or_text_frame(_rws_socket *s, _rws_frame *frame);
void rws_socket_process_ping_frame(_rws_socket *s, _rws_frame *frame);
void rws_socket_process_conn_close_frame(_rws_socket *s, _rws_frame *frame);
void rws_socket_process_received_frame(_rws_socket *s, _rws_frame *frame);
void rws_socket_idle_recv(_rws_socket *s);
void rws_socket_idle_send(_rws_socket *s);
void rws_socket_wait_handshake_responce(_rws_socket *s);
unsigned int rws_socket_get_next_message_id(_rws_socket *s);
void rws_socket_send_ping(_rws_socket *s);
void rws_socket_send_disconnect(_rws_socket *s);
void rws_socket_send_handshake(_rws_socket *s);
struct addrinfo *rws_socket_connect_getaddr_info(_rws_socket *s);
void rws_socket_connect_to_host(_rws_socket *s);
rws_bool rws_socket_create_start_work_thread(_rws_socket *s);
void rws_socket_close(_rws_socket *s);
void rws_socket_resize_received(_rws_socket *s, const size_t size);
void rws_socket_append_recvd_frames(_rws_socket *s, _rws_frame *frame);
void rws_socket_append_send_frames(_rws_socket *s, _rws_frame *frame);
rws_bool rws_socket_send_text_priv(_rws_socket *s, const char *text);
rws_bool rws_socket_send_bin_priv(_rws_socket *s, const char *data, size_t len, rws_opcode opcode, rws_bool is_fin);
void rws_socket_inform_recvd_frames(_rws_socket *s);
void rws_socket_set_option(rws_socket_t s, int option, int value);
void rws_socket_delete_all_frames_in_list(_rws_list *list_with_frames);
void rws_socket_check_write_error(_rws_socket *s, int error_num);
void rws_socket_delete(_rws_socket *s);
#ifdef LIBRWS_USING_MBED_TLS
void rws_debug(void *ctx, int level, const char *file, int line, const char *str);
int rws_ssl_conn(_rws_socket *s);
int rws_ssl_close(_rws_socket *s);
#endif
#define COMMAND_IDLE -1
#define COMMAND_NONE 0
#define COMMAND_CONNECT_TO_HOST 1
#define COMMAND_SEND_HANDSHAKE 2
#define COMMAND_WAIT_HANDSHAKE_RESPONCE 3
#define COMMAND_INFORM_CONNECTED 4
#define COMMAND_INFORM_DISCONNECTED 5
#define COMMAND_DISCONNECT 6
#define COMMAND_END 9999
#endif
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_STRING_H__
#define __RWS_STRING_H__ 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rws_common.h"
#if defined(_MSC_VER)
#define rws_sprintf(s, l, f, ...) sprintf_s(s, l, f, __VA_ARGS__)
#define rws_sscanf(s, f, ...) sscanf_s(s, f, __VA_ARGS__)
#define rws_strerror(e) strerror(e)
#else
#define rws_sprintf(s, l, f, ...) sprintf(s, f, __VA_ARGS__)
#define rws_sscanf(s, f, ...) sscanf(s, f, __VA_ARGS__)
#define rws_strerror(e) strerror(e)
#endif
char *rws_string_copy(const char *str);
char *rws_string_copy_len(const char *str, const size_t len);
void rws_string_delete(char *str);
void rws_string_delete_clean(char **str);
#endif
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __RWS_THREAD_H__
#define __RWS_THREAD_H__ 1
#include <stdio.h>
#endif