1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543 |
-
-
- enum MQTTControlPacketType {
- MQTT_CONTROL_CONNECT=1u,
- MQTT_CONTROL_CONNACK=2u,
- MQTT_CONTROL_PUBLISH=3u,
- MQTT_CONTROL_PUBACK=4u,
- MQTT_CONTROL_PUBREC=5u,
- MQTT_CONTROL_PUBREL=6u,
- MQTT_CONTROL_PUBCOMP=7u,
- MQTT_CONTROL_SUBSCRIBE=8u,
- MQTT_CONTROL_SUBACK=9u,
- MQTT_CONTROL_UNSUBSCRIBE=10u,
- MQTT_CONTROL_UNSUBACK=11u,
- MQTT_CONTROL_PINGREQ=12u,
- MQTT_CONTROL_PINGRESP=13u,
- MQTT_CONTROL_DISCONNECT=14u
- };
- struct mqtt_fixed_header {
-
- enum MQTTControlPacketType control_type;
-
-
- unsigned int control_flags: 4;
-
- uint32_t remaining_length;
- };
- MQTT_ERROR(MQTT_ERROR_NULLPTR) \
- MQTT_ERROR(MQTT_ERROR_CONTROL_FORBIDDEN_TYPE) \
- MQTT_ERROR(MQTT_ERROR_CONTROL_INVALID_FLAGS) \
- MQTT_ERROR(MQTT_ERROR_CONTROL_WRONG_TYPE) \
- MQTT_ERROR(MQTT_ERROR_CONNECT_NULL_CLIENT_ID) \
- MQTT_ERROR(MQTT_ERROR_CONNECT_NULL_WILL_MESSAGE) \
- MQTT_ERROR(MQTT_ERROR_CONNECT_FORBIDDEN_WILL_QOS) \
- MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_FLAGS) \
- MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_CODE) \
- MQTT_ERROR(MQTT_ERROR_PUBLISH_FORBIDDEN_QOS) \
- MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_TOO_MANY_TOPICS) \
- MQTT_ERROR(MQTT_ERROR_MALFORMED_RESPONSE) \
- MQTT_ERROR(MQTT_ERROR_UNSUBSCRIBE_TOO_MANY_TOPICS) \
- MQTT_ERROR(MQTT_ERROR_RESPONSE_INVALID_CONTROL_TYPE) \
- MQTT_ERROR(MQTT_ERROR_CONNECT_NOT_CALLED) \
- MQTT_ERROR(MQTT_ERROR_SEND_BUFFER_IS_FULL) \
- MQTT_ERROR(MQTT_ERROR_SOCKET_ERROR) \
- MQTT_ERROR(MQTT_ERROR_MALFORMED_REQUEST) \
- MQTT_ERROR(MQTT_ERROR_RECV_BUFFER_TOO_SMALL) \
- MQTT_ERROR(MQTT_ERROR_ACK_OF_UNKNOWN) \
- MQTT_ERROR(MQTT_ERROR_NOT_IMPLEMENTED) \
- MQTT_ERROR(MQTT_ERROR_CONNECTION_REFUSED) \
- MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_FAILED) \
- MQTT_ERROR(MQTT_ERROR_CONNECTION_CLOSED) \
- MQTT_ERROR(MQTT_ERROR_INITIAL_RECONNECT) \
- MQTT_ERROR(MQTT_ERROR_INVALID_REMAINING_LENGTH)
- enum MQTTErrors {
- MQTT_ERROR_UNKNOWN=INT_MIN,
- __ALL_MQTT_ERRORS(GENERATE_ENUM)
- MQTT_OK = 1
- };
- const char* mqtt_error_str(enum MQTTErrors error);
- ssize_t __mqtt_pack_str(uint8_t *buf, const char* str);
- enum MQTTConnackReturnCode {
- MQTT_CONNACK_ACCEPTED = 0u,
- MQTT_CONNACK_REFUSED_PROTOCOL_VERSION = 1u,
- MQTT_CONNACK_REFUSED_IDENTIFIER_REJECTED = 2u,
- MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE = 3u,
- MQTT_CONNACK_REFUSED_BAD_USER_NAME_OR_PASSWORD = 4u,
- MQTT_CONNACK_REFUSED_NOT_AUTHORIZED = 5u
- };
- struct mqtt_response_connack {
-
- uint8_t session_present_flag;
-
- enum MQTTConnackReturnCode return_code;
- };
-
- struct mqtt_response_publish {
-
- uint8_t dup_flag;
-
- uint8_t qos_level;
-
- uint8_t retain_flag;
-
- uint16_t topic_name_size;
-
- const void* topic_name;
-
- uint16_t packet_id;
-
- const void* application_message;
-
- size_t application_message_size;
- };
- struct mqtt_response_puback {
-
- uint16_t packet_id;
- };
- struct mqtt_response_pubrec {
-
- uint16_t packet_id;
- };
- struct mqtt_response_pubrel {
-
- uint16_t packet_id;
- };
- struct mqtt_response_pubcomp {
-
- uint16_t packet_id;
- };
- enum MQTTSubackReturnCodes {
- MQTT_SUBACK_SUCCESS_MAX_QOS_0 = 0u,
- MQTT_SUBACK_SUCCESS_MAX_QOS_1 = 1u,
- MQTT_SUBACK_SUCCESS_MAX_QOS_2 = 2u,
- MQTT_SUBACK_FAILURE = 128u
- };
- struct mqtt_response_suback {
-
- uint16_t packet_id;
-
- const uint8_t *return_codes;
-
- size_t num_return_codes;
- };
- struct mqtt_response_unsuback {
-
- uint16_t packet_id;
- };
- struct mqtt_response_pingresp {int dummy;};
- struct mqtt_response {
-
- struct mqtt_fixed_header fixed_header;
-
- union {
- struct mqtt_response_connack connack;
- struct mqtt_response_publish publish;
- struct mqtt_response_puback puback;
- struct mqtt_response_pubrec pubrec;
- struct mqtt_response_pubrel pubrel;
- struct mqtt_response_pubcomp pubcomp;
- struct mqtt_response_suback suback;
- struct mqtt_response_unsuback unsuback;
- struct mqtt_response_pingresp pingresp;
- } decoded;
- };
- ssize_t mqtt_unpack_fixed_header(struct mqtt_response *response, const uint8_t *buf, size_t bufsz);
- ssize_t mqtt_unpack_connack_response (struct mqtt_response *mqtt_response, const uint8_t *buf);
- ssize_t mqtt_unpack_publish_response (struct mqtt_response *mqtt_response, const uint8_t *buf);
- ssize_t mqtt_unpack_pubxxx_response(struct mqtt_response *mqtt_response, const uint8_t *buf);
- ssize_t mqtt_unpack_suback_response(struct mqtt_response *mqtt_response, const uint8_t *buf);
-
- ssize_t mqtt_unpack_unsuback_response(struct mqtt_response *mqtt_response, const uint8_t *buf);
- ssize_t mqtt_unpack_response(struct mqtt_response* response, const uint8_t *buf, size_t bufsz);
-
- ssize_t mqtt_pack_fixed_header(uint8_t *buf, size_t bufsz, const struct mqtt_fixed_header *fixed_header);
- enum MQTTConnectFlags {
- MQTT_CONNECT_RESERVED = 1u,
- MQTT_CONNECT_CLEAN_SESSION = 2u,
- MQTT_CONNECT_WILL_FLAG = 4u,
- MQTT_CONNECT_WILL_QOS_0 = (0u & 0x03) << 3,
- MQTT_CONNECT_WILL_QOS_1 = (1u & 0x03) << 3,
- MQTT_CONNECT_WILL_QOS_2 = (2u & 0x03) << 3,
- MQTT_CONNECT_WILL_RETAIN = 32u,
- MQTT_CONNECT_PASSWORD = 64u,
- MQTT_CONNECT_USER_NAME = 128u,
- };
- ssize_t mqtt_pack_connection_request(uint8_t* buf, size_t bufsz,
- const char* client_id,
- const char* will_topic,
- const void* will_message,
- size_t will_message_size,
- const char* user_name,
- const char* password,
- uint8_t connect_flags,
- uint16_t keep_alive);
- enum MQTTPublishFlags {
- MQTT_PUBLISH_DUP = 8u,
- MQTT_PUBLISH_QOS_0 = ((0u << 1) & 0x06),
- MQTT_PUBLISH_QOS_1 = ((1u << 1) & 0x06),
- MQTT_PUBLISH_QOS_2 = ((2u << 1) & 0x06),
- MQTT_PUBLISH_QOS_MASK = ((3u << 1) & 0x06),
- MQTT_PUBLISH_RETAIN = 0x01
- };
- ssize_t mqtt_pack_publish_request(uint8_t *buf, size_t bufsz,
- const char* topic_name,
- uint16_t packet_id,
- void* application_message,
- size_t application_message_size,
- uint8_t publish_flags);
- ssize_t mqtt_pack_pubxxx_request(uint8_t *buf, size_t bufsz,
- enum MQTTControlPacketType control_type,
- uint16_t packet_id);
- ssize_t mqtt_pack_subscribe_request(uint8_t *buf, size_t bufsz,
- uint16_t packet_id,
- ...);
- ssize_t mqtt_pack_unsubscribe_request(uint8_t *buf, size_t bufsz,
- uint16_t packet_id,
- ...);
- ssize_t mqtt_pack_ping_request(uint8_t *buf, size_t bufsz);
- ssize_t mqtt_pack_disconnect(uint8_t *buf, size_t bufsz);
- enum MQTTQueuedMessageState {
- MQTT_QUEUED_UNSENT,
- MQTT_QUEUED_AWAITING_ACK,
- MQTT_QUEUED_COMPLETE
- };
- struct mqtt_queued_message {
-
- uint8_t *start;
-
- size_t size;
-
- enum MQTTQueuedMessageState state;
-
- mqtt_pal_time_t time_sent;
-
- enum MQTTControlPacketType control_type;
-
- uint16_t packet_id;
- };
- struct mqtt_message_queue {
-
- void *mem_start;
-
- void *mem_end;
-
- uint8_t *curr;
-
- size_t curr_sz;
-
-
- struct mqtt_queued_message *queue_tail;
- };
- void mqtt_mq_init(struct mqtt_message_queue *mq, void *buf, size_t bufsz);
- void mqtt_mq_clean(struct mqtt_message_queue *mq);
- struct mqtt_queued_message* mqtt_mq_register(struct mqtt_message_queue *mq, size_t nbytes);
- struct mqtt_queued_message* mqtt_mq_find(struct mqtt_message_queue *mq, enum MQTTControlPacketType control_type, uint16_t *packet_id);
- struct mqtt_client {
-
- mqtt_pal_socket_handle socketfd;
-
- uint16_t pid_lfsr;
-
- uint16_t keep_alive;
-
- int number_of_keep_alives;
-
- mqtt_pal_time_t time_of_last_send;
-
- enum MQTTErrors error;
-
- int response_timeout;
-
- int number_of_timeouts;
-
- double typical_response_time;
-
- void (*publish_response_callback)(void** state, struct mqtt_response_publish *publish);
-
- void* publish_response_callback_state;
-
- enum MQTTErrors (*inspector_callback)(struct mqtt_client*);
-
- void (*reconnect_callback)(struct mqtt_client*, void**);
-
- void* reconnect_state;
-
- struct {
-
- uint8_t *mem_start;
-
- size_t mem_size;
-
- uint8_t *curr;
-
- size_t curr_sz;
- } recv_buffer;
-
- mqtt_pal_mutex_t mutex;
-
- struct mqtt_message_queue mq;
- };
- uint16_t __mqtt_next_pid(struct mqtt_client *client);
- ssize_t __mqtt_send(struct mqtt_client *client);
- ssize_t __mqtt_recv(struct mqtt_client *client);
- enum MQTTErrors mqtt_sync(struct mqtt_client *client);
- enum MQTTErrors mqtt_init(struct mqtt_client *client,
- mqtt_pal_socket_handle sockfd,
- uint8_t *sendbuf, size_t sendbufsz,
- uint8_t *recvbuf, size_t recvbufsz,
- void (*publish_response_callback)(void** state, struct mqtt_response_publish *publish));
- void mqtt_init_reconnect(struct mqtt_client *client,
- void (*reconnect_callback)(struct mqtt_client *client, void** state),
- void *reconnect_state,
- void (*publish_response_callback)(void** state, struct mqtt_response_publish *publish));
- void mqtt_reinit(struct mqtt_client* client,
- mqtt_pal_socket_handle socketfd,
- uint8_t *sendbuf, size_t sendbufsz,
- uint8_t *recvbuf, size_t recvbufsz);
- enum MQTTErrors mqtt_connect(struct mqtt_client *client,
- const char* client_id,
- const char* will_topic,
- const void* will_message,
- size_t will_message_size,
- const char* user_name,
- const char* password,
- uint8_t connect_flags,
- uint16_t keep_alive);
- enum MQTTErrors mqtt_publish(struct mqtt_client *client,
- const char* topic_name,
- void* application_message,
- size_t application_message_size,
- uint8_t publish_flags);
- ssize_t __mqtt_puback(struct mqtt_client *client, uint16_t packet_id);
- ssize_t __mqtt_pubrec(struct mqtt_client *client, uint16_t packet_id);
- ssize_t __mqtt_pubrel(struct mqtt_client *client, uint16_t packet_id);
- ssize_t __mqtt_pubcomp(struct mqtt_client *client, uint16_t packet_id);
- enum MQTTErrors mqtt_subscribe(struct mqtt_client *client,
- const char* topic_name,
- int max_qos_level);
- enum MQTTErrors mqtt_unsubscribe(struct mqtt_client *client,
- const char* topic_name);
- enum MQTTErrors mqtt_ping(struct mqtt_client *client);
- enum MQTTErrors __mqtt_ping(struct mqtt_client *client);
- enum MQTTErrors mqtt_disconnect(struct mqtt_client *client);
|