Part of TI's mqtt

Dependents:   mqtt_V1 cc3100_Test_mqtt_CM3

Committer:
dflet
Date:
Sat Jun 06 13:29:08 2015 +0000
Revision:
0:547251f42a60
Part of mqtt_V1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:547251f42a60 1 /******************************************************************************
dflet 0:547251f42a60 2 *
dflet 0:547251f42a60 3 * Copyright (C) 2014 Texas Instruments Incorporated
dflet 0:547251f42a60 4 *
dflet 0:547251f42a60 5 * All rights reserved. Property of Texas Instruments Incorporated.
dflet 0:547251f42a60 6 * Restricted rights to use, duplicate or disclose this code are
dflet 0:547251f42a60 7 * granted through contract.
dflet 0:547251f42a60 8 *
dflet 0:547251f42a60 9 * The program may not be used without the written permission of
dflet 0:547251f42a60 10 * Texas Instruments Incorporated or against the terms and conditions
dflet 0:547251f42a60 11 * stipulated in the agreement under which this program has been supplied,
dflet 0:547251f42a60 12 * and under no circumstances can it be used with non-TI connectivity device.
dflet 0:547251f42a60 13 *
dflet 0:547251f42a60 14 ******************************************************************************/
dflet 0:547251f42a60 15
dflet 0:547251f42a60 16 #include "server_util.h"
dflet 0:547251f42a60 17
dflet 0:547251f42a60 18 namespace mbed_mqtt {
dflet 0:547251f42a60 19
dflet 0:547251f42a60 20 static uint16_t msg_id = 0xFFFF;
dflet 0:547251f42a60 21 static inline uint16_t assign_new_msg_id()
dflet 0:547251f42a60 22 {
dflet 0:547251f42a60 23 return msg_id += 2;
dflet 0:547251f42a60 24 }
dflet 0:547251f42a60 25
dflet 0:547251f42a60 26 uint16_t mqp_new_id_server(void)
dflet 0:547251f42a60 27 {
dflet 0:547251f42a60 28 return assign_new_msg_id();
dflet 0:547251f42a60 29 }
dflet 0:547251f42a60 30
dflet 0:547251f42a60 31 static void my_pkt_free(struct mqtt_packet *mqp)
dflet 0:547251f42a60 32 {
dflet 0:547251f42a60 33 my_free((void*) mqp);
dflet 0:547251f42a60 34 }
dflet 0:547251f42a60 35
dflet 0:547251f42a60 36 /*----------------------------------------------------------------------------
dflet 0:547251f42a60 37 * Try to prevent fragmentation by consistently allocating a fixed memory size
dflet 0:547251f42a60 38 *----------------------------------------------------------------------------
dflet 0:547251f42a60 39 */
dflet 0:547251f42a60 40 #ifndef CFG_SR_MAX_MQP_TX_LEN
dflet 0:547251f42a60 41 #define MQP_SERVER_TX_LEN 1024
dflet 0:547251f42a60 42 #else
dflet 0:547251f42a60 43 #define MQP_SERVER_TX_LEN CFG_SR_MAX_MQP_TX_LEN
dflet 0:547251f42a60 44 #endif
dflet 0:547251f42a60 45
dflet 0:547251f42a60 46 /*------------------ Fixed memory configuration ends -----------------------*/
dflet 0:547251f42a60 47
dflet 0:547251f42a60 48 static struct mqtt_packet *server_mqp_alloc(uint8_t msg_type, uint32_t buf_sz, uint8_t offset)
dflet 0:547251f42a60 49 {
dflet 0:547251f42a60 50 uint32_t mqp_sz = sizeof(struct mqtt_packet);
dflet 0:547251f42a60 51 struct mqtt_packet *mqp = NULL;
dflet 0:547251f42a60 52
dflet 0:547251f42a60 53 buf_sz += offset;
dflet 0:547251f42a60 54
dflet 0:547251f42a60 55 if((mqp_sz + buf_sz) > MQP_SERVER_TX_LEN) {
dflet 0:547251f42a60 56 USR_INFO("S: fatal, buf alloc len > MQP_SERVER_TX_LEN\n\r");
dflet 0:547251f42a60 57 return NULL;
dflet 0:547251f42a60 58 }
dflet 0:547251f42a60 59
dflet 0:547251f42a60 60 mqp = (mqtt_packet*)my_malloc(MQP_SERVER_TX_LEN);
dflet 0:547251f42a60 61 if(NULL != mqp) {
dflet 0:547251f42a60 62 mqp_init(mqp, offset);
dflet 0:547251f42a60 63
dflet 0:547251f42a60 64 mqp->msg_type = msg_type;
dflet 0:547251f42a60 65 mqp->maxlen = buf_sz;
dflet 0:547251f42a60 66 mqp->buffer = (uint8_t*)mqp + mqp_sz;
dflet 0:547251f42a60 67
dflet 0:547251f42a60 68 mqp->free = my_pkt_free;
dflet 0:547251f42a60 69
dflet 0:547251f42a60 70 } else {
dflet 0:547251f42a60 71 USR_INFO("S: fatal, failed to alloc Server MQP\n\r");
dflet 0:547251f42a60 72 }
dflet 0:547251f42a60 73
dflet 0:547251f42a60 74 return mqp;
dflet 0:547251f42a60 75 }
dflet 0:547251f42a60 76
dflet 0:547251f42a60 77 struct mqtt_packet *mqp_server_alloc(uint8_t msg_type, uint32_t buf_sz)
dflet 0:547251f42a60 78 {
dflet 0:547251f42a60 79 return server_mqp_alloc(msg_type, buf_sz, MAX_FH_LEN);
dflet 0:547251f42a60 80 }
dflet 0:547251f42a60 81
dflet 0:547251f42a60 82 struct mqtt_packet *mqp_server_copy(const struct mqtt_packet *mqp)
dflet 0:547251f42a60 83 {
dflet 0:547251f42a60 84 struct mqtt_packet *cpy = server_mqp_alloc(mqp->msg_type,
dflet 0:547251f42a60 85 mqp->maxlen, 0);
dflet 0:547251f42a60 86 if(NULL != cpy) {
dflet 0:547251f42a60 87 uint8_t *buffer = cpy->buffer;
dflet 0:547251f42a60 88
dflet 0:547251f42a60 89 /* Copy to overwrite everything in 'cpy' from source 'mqp' */
dflet 0:547251f42a60 90 buf_wr_nbytes((uint8_t*)cpy, (uint8_t*)mqp, sizeof(struct mqtt_packet));
dflet 0:547251f42a60 91
dflet 0:547251f42a60 92 cpy->buffer = buffer; /* Restore buffer and copy */
dflet 0:547251f42a60 93 buf_wr_nbytes(cpy->buffer, mqp->buffer, mqp->maxlen);
dflet 0:547251f42a60 94 }
dflet 0:547251f42a60 95
dflet 0:547251f42a60 96 return cpy;
dflet 0:547251f42a60 97 }
dflet 0:547251f42a60 98
dflet 0:547251f42a60 99 int32_t (*util_dbg_prn)(const char *fmt, ...) = NULL;
dflet 0:547251f42a60 100 bool util_prn_aux = false;
dflet 0:547251f42a60 101
dflet 0:547251f42a60 102 static void (*lockin_mutex)(void*) = NULL;
dflet 0:547251f42a60 103 static void (*unlock_mutex)(void*) = NULL;
dflet 0:547251f42a60 104 static void *mutex_hnd = NULL;
dflet 0:547251f42a60 105
dflet 0:547251f42a60 106 void util_mutex_lockin(void)
dflet 0:547251f42a60 107 {
dflet 0:547251f42a60 108 if(lockin_mutex)
dflet 0:547251f42a60 109 lockin_mutex(mutex_hnd);
dflet 0:547251f42a60 110
dflet 0:547251f42a60 111 return;
dflet 0:547251f42a60 112 }
dflet 0:547251f42a60 113
dflet 0:547251f42a60 114 void util_mutex_unlock(void)
dflet 0:547251f42a60 115 {
dflet 0:547251f42a60 116 if(unlock_mutex)
dflet 0:547251f42a60 117 unlock_mutex(mutex_hnd);
dflet 0:547251f42a60 118
dflet 0:547251f42a60 119 return;
dflet 0:547251f42a60 120 }
dflet 0:547251f42a60 121
dflet 0:547251f42a60 122 void util_params_set(int32_t (*dbg_prn)(const char *fmt, ...),
dflet 0:547251f42a60 123 void *mutex,
dflet 0:547251f42a60 124 void (*mutex_lockin)(void*),
dflet 0:547251f42a60 125 void (*mutex_unlock)(void*))
dflet 0:547251f42a60 126 {
dflet 0:547251f42a60 127 util_dbg_prn = dbg_prn;
dflet 0:547251f42a60 128 mutex_hnd = mutex;
dflet 0:547251f42a60 129 lockin_mutex = mutex_lockin;
dflet 0:547251f42a60 130 unlock_mutex = mutex_unlock;
dflet 0:547251f42a60 131
dflet 0:547251f42a60 132 return;
dflet 0:547251f42a60 133 }
dflet 0:547251f42a60 134
dflet 0:547251f42a60 135 }//namespace mbed_mqtt {