The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.
Fork of Cayenne-MQTT-mbed by
src/CayenneUtils/CayenneUtils.h@0:09ef59d2d0f7, 2016-10-07 (annotated)
- Committer:
- jburhenn
- Date:
- Fri Oct 07 17:21:45 2016 +0000
- Revision:
- 0:09ef59d2d0f7
- Child:
- 1:90dccf306268
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:09ef59d2d0f7 | 1 | /* |
jburhenn | 0:09ef59d2d0f7 | 2 | The MIT License(MIT) |
jburhenn | 0:09ef59d2d0f7 | 3 | |
jburhenn | 0:09ef59d2d0f7 | 4 | Cayenne MQTT Client Library |
jburhenn | 0:09ef59d2d0f7 | 5 | Copyright (c) 2016 myDevices |
jburhenn | 0:09ef59d2d0f7 | 6 | |
jburhenn | 0:09ef59d2d0f7 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
jburhenn | 0:09ef59d2d0f7 | 8 | documentation files(the "Software"), to deal in the Software without restriction, including without limitation |
jburhenn | 0:09ef59d2d0f7 | 9 | the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, |
jburhenn | 0:09ef59d2d0f7 | 10 | and to permit persons to whom the Software is furnished to do so, subject to the following conditions : |
jburhenn | 0:09ef59d2d0f7 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
jburhenn | 0:09ef59d2d0f7 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
jburhenn | 0:09ef59d2d0f7 | 13 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR |
jburhenn | 0:09ef59d2d0f7 | 14 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
jburhenn | 0:09ef59d2d0f7 | 15 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jburhenn | 0:09ef59d2d0f7 | 16 | |
jburhenn | 0:09ef59d2d0f7 | 17 | */ |
jburhenn | 0:09ef59d2d0f7 | 18 | |
jburhenn | 0:09ef59d2d0f7 | 19 | #ifndef _CAYENNEUTILS_h |
jburhenn | 0:09ef59d2d0f7 | 20 | #define _CAYENNEUTILS_h |
jburhenn | 0:09ef59d2d0f7 | 21 | |
jburhenn | 0:09ef59d2d0f7 | 22 | #if defined(__cplusplus) |
jburhenn | 0:09ef59d2d0f7 | 23 | extern "C" { |
jburhenn | 0:09ef59d2d0f7 | 24 | #endif |
jburhenn | 0:09ef59d2d0f7 | 25 | |
jburhenn | 0:09ef59d2d0f7 | 26 | #if defined(WIN32_DLL) || defined(WIN64_DLL) |
jburhenn | 0:09ef59d2d0f7 | 27 | #define DLLImport __declspec(dllimport) |
jburhenn | 0:09ef59d2d0f7 | 28 | #define DLLExport __declspec(dllexport) |
jburhenn | 0:09ef59d2d0f7 | 29 | #elif defined(LINUX_SO) |
jburhenn | 0:09ef59d2d0f7 | 30 | #define DLLImport extern |
jburhenn | 0:09ef59d2d0f7 | 31 | #define DLLExport __attribute__ ((visibility ("default"))) |
jburhenn | 0:09ef59d2d0f7 | 32 | #else |
jburhenn | 0:09ef59d2d0f7 | 33 | #define DLLImport |
jburhenn | 0:09ef59d2d0f7 | 34 | #define DLLExport |
jburhenn | 0:09ef59d2d0f7 | 35 | #endif |
jburhenn | 0:09ef59d2d0f7 | 36 | |
jburhenn | 0:09ef59d2d0f7 | 37 | #include <stdio.h> |
jburhenn | 0:09ef59d2d0f7 | 38 | #include "CayenneDefines.h" |
jburhenn | 0:09ef59d2d0f7 | 39 | |
jburhenn | 0:09ef59d2d0f7 | 40 | enum CayenneReturnCode { CAYENNE_BUFFER_OVERFLOW = -2, CAYENNE_FAILURE = -1, CAYENNE_SUCCESS = 0 }; |
jburhenn | 0:09ef59d2d0f7 | 41 | |
jburhenn | 0:09ef59d2d0f7 | 42 | typedef struct CayenneValuePair |
jburhenn | 0:09ef59d2d0f7 | 43 | { |
jburhenn | 0:09ef59d2d0f7 | 44 | const char* unit; |
jburhenn | 0:09ef59d2d0f7 | 45 | const char* value; |
jburhenn | 0:09ef59d2d0f7 | 46 | } CayenneValuePair; |
jburhenn | 0:09ef59d2d0f7 | 47 | |
jburhenn | 0:09ef59d2d0f7 | 48 | /** |
jburhenn | 0:09ef59d2d0f7 | 49 | * Build a specified topic string. |
jburhenn | 0:09ef59d2d0f7 | 50 | * @param[out] topicName Returned topic string |
jburhenn | 0:09ef59d2d0f7 | 51 | * @param[in] length CayenneTopic buffer length |
jburhenn | 0:09ef59d2d0f7 | 52 | * @param[in] username Cayenne username |
jburhenn | 0:09ef59d2d0f7 | 53 | * @param[in] clientID Cayennne client ID |
jburhenn | 0:09ef59d2d0f7 | 54 | * @param[in] topic Cayenne topic |
jburhenn | 0:09ef59d2d0f7 | 55 | * @param[in] channel The topic channel, use CAYENNE_NO_CHANNEL if none is required, CAYENNE_ALL_CHANNELS if a wildcard is required |
jburhenn | 0:09ef59d2d0f7 | 56 | * @return CAYENNE_SUCCESS if topic string was created, error code otherwise |
jburhenn | 0:09ef59d2d0f7 | 57 | */ |
jburhenn | 0:09ef59d2d0f7 | 58 | DLLExport int CayenneBuildTopic(char* topicName, size_t length, const char* username, const char* clientID, CayenneTopic topic, unsigned int channel); |
jburhenn | 0:09ef59d2d0f7 | 59 | |
jburhenn | 0:09ef59d2d0f7 | 60 | /** |
jburhenn | 0:09ef59d2d0f7 | 61 | * Build a specified data payload. |
jburhenn | 0:09ef59d2d0f7 | 62 | * @param[out] payload Returned payload |
jburhenn | 0:09ef59d2d0f7 | 63 | * @param[in,out] length Payload buffer length |
jburhenn | 0:09ef59d2d0f7 | 64 | * @param[in] type Optional type to use for type,unit=value payload, can be NULL |
jburhenn | 0:09ef59d2d0f7 | 65 | * @param[in] values Unit/value array |
jburhenn | 0:09ef59d2d0f7 | 66 | * @param[in] valueCount Number of values |
jburhenn | 0:09ef59d2d0f7 | 67 | * @return CAYENNE_SUCCESS if topic string was created, error code otherwise |
jburhenn | 0:09ef59d2d0f7 | 68 | */ |
jburhenn | 0:09ef59d2d0f7 | 69 | DLLExport int CayenneBuildDataPayload(char* payload, size_t* length, const char* type, const CayenneValuePair* values, size_t valueCount); |
jburhenn | 0:09ef59d2d0f7 | 70 | |
jburhenn | 0:09ef59d2d0f7 | 71 | /** |
jburhenn | 0:09ef59d2d0f7 | 72 | * Build a specified response payload. |
jburhenn | 0:09ef59d2d0f7 | 73 | * @param[out] payload Returned payload |
jburhenn | 0:09ef59d2d0f7 | 74 | * @param[in,out] length Payload buffer length |
jburhenn | 0:09ef59d2d0f7 | 75 | * @param[in] id ID of message the response is for |
jburhenn | 0:09ef59d2d0f7 | 76 | * @param[in] error Optional error message, NULL for success |
jburhenn | 0:09ef59d2d0f7 | 77 | * @return CAYENNE_SUCCESS if topic string was created, error code otherwise |
jburhenn | 0:09ef59d2d0f7 | 78 | */ |
jburhenn | 0:09ef59d2d0f7 | 79 | DLLExport int CayenneBuildResponsePayload(char* payload, size_t* length, const char* id, const char* error); |
jburhenn | 0:09ef59d2d0f7 | 80 | |
jburhenn | 0:09ef59d2d0f7 | 81 | /** |
jburhenn | 0:09ef59d2d0f7 | 82 | * Parse a topic string in place. This may modify the topic string. |
jburhenn | 0:09ef59d2d0f7 | 83 | * @param[out] topic Returned Cayenne topic |
jburhenn | 0:09ef59d2d0f7 | 84 | * @param[out] channel Returned channel, CAYENNE_NO_CHANNEL if there is none |
jburhenn | 0:09ef59d2d0f7 | 85 | * @param[out] clientID Returned client ID |
jburhenn | 0:09ef59d2d0f7 | 86 | * @param[in] username Cayenne username |
jburhenn | 0:09ef59d2d0f7 | 87 | * @param[in] topicName Topic name string |
jburhenn | 0:09ef59d2d0f7 | 88 | * @param[in] length Topic name string length |
jburhenn | 0:09ef59d2d0f7 | 89 | * @return CAYENNE_SUCCESS if topic was parsed, error code otherwise |
jburhenn | 0:09ef59d2d0f7 | 90 | */ |
jburhenn | 0:09ef59d2d0f7 | 91 | DLLExport int CayenneParseTopic(CayenneTopic* topic, unsigned int* channel, const char** clientID, const char* username, char* topicName, unsigned int length); |
jburhenn | 0:09ef59d2d0f7 | 92 | |
jburhenn | 0:09ef59d2d0f7 | 93 | /** |
jburhenn | 0:09ef59d2d0f7 | 94 | * Parse a null terminated payload in place. This may modify the payload string. |
jburhenn | 0:09ef59d2d0f7 | 95 | * @param[out] values Returned payload data unit & value array |
jburhenn | 0:09ef59d2d0f7 | 96 | * @param[in,out] valuesSize Size of values array, returns the count of values in the array |
jburhenn | 0:09ef59d2d0f7 | 97 | * @param[out] type Returned type, NULL if there is none |
jburhenn | 0:09ef59d2d0f7 | 98 | * @param[out] id Returned message id, empty string if there is none |
jburhenn | 0:09ef59d2d0f7 | 99 | * @param[in] topic Cayenne topic |
jburhenn | 0:09ef59d2d0f7 | 100 | * @param[in] payload Payload string, must be null terminated. |
jburhenn | 0:09ef59d2d0f7 | 101 | * @return CAYENNE_SUCCESS if topic string was created, error code otherwise |
jburhenn | 0:09ef59d2d0f7 | 102 | */ |
jburhenn | 0:09ef59d2d0f7 | 103 | DLLExport int CayenneParsePayload(CayenneValuePair* values, size_t* valuesSize, const char** type, const char** id, CayenneTopic topic, char* payload); |
jburhenn | 0:09ef59d2d0f7 | 104 | |
jburhenn | 0:09ef59d2d0f7 | 105 | #if defined(__cplusplus) |
jburhenn | 0:09ef59d2d0f7 | 106 | } |
jburhenn | 0:09ef59d2d0f7 | 107 | #endif |
jburhenn | 0:09ef59d2d0f7 | 108 | |
jburhenn | 0:09ef59d2d0f7 | 109 | #endif |