MQTT-C-UDP_PUB
/**
******************************************************************************
* @file apdu.c
* @author ding
* @version V1.0.0
* @date 2016-10-25
* @brief This file provides all the decode or encode functions.
******************************************************************************
* @attention
*
* FIX ME.
*
* <h2><center>© COPYRIGHT 2016 THIT</center></h2>
******************************************************************************
*/
/** @mainpage
* <span style="color: red; font-size: 20pt; font-family: 脫脳脭虏">
* <CENTER>CRRC Information Technology Co.,Ltd</CENTER>
* </span>
* <span style="font-size: 14pt; font-family: 驴卢脤氓_GB2312">
* <CENTER>Automatic Test Equipment</CENTER>
* <br/>
* </span>
*<hr/>
*<span style="font-size:12pt;"> </span>
*This documents the automatic test equipment API and sample applications.
*<hr/>
* <br/>
* <span style="font-size: 10pt; font-family: 潞脷脤氓">
* <CENTER>Advanced Research Institute</CENTER>
* </span>
* <br/>
* <span style="font-size: 10pt; font-family: 潞脷脤氓">
* <CENTER>Advanced Research Institute</CENTER>
* <CENTER>Mu HongWei</CENTER>
* <CENTER>2016-10-25</CENTER>
*<CENTER><a href="http://www.thit.com.cn/">Welcome To Visit THIT</a></CENTER>
* </span>
* <br/>
*/
/* Includes ------------------------------------------------------------------*/
#include "encode.h"
#include <stdio.h>
#include <stdlib.h>
#include "sent_request.h"
#include "sent_ack.h"
#include "sent_error.h"
#include "sent_apdu.h"
#include "request_handler.h"
#include "tcp_setup.h"
#include "decrypt.h"
#include <sys/stat.h>
//#include <sys/time.h> /* CLOCK_MONOTONIC */
#include <time.h> /* clock_gettime() CLOCK_MONOTONIC*/
#include <termios.h> /* struct termios */
#include <string.h> /* strcasecmp() */
#include <unistd.h> /* usleep() */
#include <arpa/inet.h> /* htons() */
#include <pthread.h>
//---------------------------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
///////////////////////////////////////////////////////----------------------MQTT-------------------------///////////////////////////////////////////////////////////////////////////////////////////////////////
/* This provides a crude manner of testing the performance of a broker in messages/s. */
#include <stdbool.h>
#include <stdint.h>
//#include <stdio.h>
#include <sys/time.h>
#include <mosquitto.h>
#include<mosquitto_internal.h>
#include "msgsps_common.h"
#include<time.h>
#include <string.h>
static unsigned char heart_num;
static bool run = true;
static int message_count = 0;
static struct timeval start, stop;
//static struct mosquitto *mosq;
//static char *buf123="0123456789";
//static unsigned int mqtt_num =0;
//static unsigned int message_count=0;
//-----------------------------------------------------------------------
static unsigned int ip_addr;
static unsigned short int port;
//------------------------------------------------------------------------
/*
============================================================================
Name : mqtest.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
#include <string.h>
static int x =1;
/*static lgt*/ struct mosquitto *mosq = NULL;
int i_count=0;//lgt
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
if(message->payloadlen){
printf("%s %s\n", message->topic, message->payload);
}else{
printf("%s (null)\n", message->topic);
}
fflush(stdout);
}
void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
int i;
if(!result){
mosquitto_subscribe(mosq, NULL, "mqtt-test-queue", 2);
/*mosquitto_publish(mosq,NULL,"lgt")*/
/*int payloadlen, const void *payload, int qos, bool retain*/
}else{
fprintf(stderr, "Connect failed\n");
}
}
void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for(i=1; i<qos_count; i++){
printf(", %d", granted_qos[i]);
}
printf("\n");
}
void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
/* Pring all log messages regardless of level.*/
printf("%s\n", str);
}
static void MQTT_topic_data(unsigned char *data,int data_len)
{
//puts("!!!Hello World!!!");
//* prints !!!Hello World!!!
printf("\ndata-----------------------------------------------------MQTT-----------------------------------------------------------------\n");
char dst1[20];
unsigned int i;
unsigned char str[1024];
unsigned char dst[1024];
for(i=0;i<data_len;i++)
{
str[2*i] = data[i] >>4;
str[2*i+1] = data[i] & 0xf;
}
for(i=0;i<2*data_len;i++)
{
sprintf(&dst[i],"%X/n",str[i]);
printf("%c ",dst[i]);
}
i_count++;
sprintf(dst1, "%d", i_count);
/**ddd=strcat(dst, dst1);*/
//dst1=dst;
//if (data_len>0)
// {
mosquitto_publish(mosq, NULL, "topic-from-kenya",2*data_len, dst, 0, true);
// }
// {
//mosquitto_publish(mosq, NULL, "mqtt-test-queue",strlen(dst1), dst1, 0, true);
// sleep(1);
// }
}
///////////////////////////////////////////////////////////////////--------------------MQTT-TAIL---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////
//static struct circ_buf recv_queue = {PTHREAD_MUTEX_INITIALIZER, 0, 0};
static packet_info recv_packet[TRAIN_TOTAL];
static int cmp_addr(struct sockaddr_in dest, struct sockaddr_in src)
{
//-----------------------------------------------
ip_addr=src.sin_addr.s_addr;
port =src.sin_port;
//----------------------------------------------
char dst[1000];
//-----------------------------------------------
// printf("dest addr: %d--%d--%d\n", dest.sin_family, dest.sin_addr.s_addr, dest.sin_port);
// printf("src addr: %d--%d--%d\n", src.sin_family, src.sin_addr.s_addr, src.sin_port);
//-----------------------------------------------------
if((inet_ntop(AF_INET,&src.sin_addr.s_addr,dst,sizeof(dst)))==NULL)
printf("inet_ntop\n");
printf("dst=%s,sizeof(dst)=%d\n",dst,sizeof(dst));
//------------------------------------------------------
if ((dest.sin_family != src.sin_family) || (dest.sin_addr.s_addr != src.sin_addr.s_addr) || (dest.sin_port != src.sin_port))
return -1;
else
return 0;
}
static void int_packet(void)
{
int i;
for (i = 0; i < TRAIN_TOTAL; i++) {
memset(&(recv_packet[i].dout), '\0', sizeof(struct sockaddr_in));
recv_packet[i].packet_total = 0;
recv_packet[i].state = 0;
}
}
static int set_packet_total(struct sockaddr_in addr)
{
int i;
for (i = 0; i < TRAIN_TOTAL; i++) {
if ((cmp_addr(recv_packet[i].dout, addr) == 0) && (recv_packet[i].state == 1)) {
recv_packet[i].packet_total++;
printf("find sockaddr_in!\n");
return 1;
}
}
for (i = 0; i < TRAIN_TOTAL; i++) {
if (recv_packet[i].state == 0) {
//memcpy(&(recv_packet[i].dout), &addr, sizeof(struct sockaddr_in));
recv_packet[i].dout.sin_family = addr.sin_family;
recv_packet[i].dout.sin_addr.s_addr = addr.sin_addr.s_addr;
recv_packet[i].dout.sin_port = addr.sin_port;
recv_packet[i].packet_total = 1;
recv_packet[i].state = 1;
// printf("dest port: %d, src port: %d\n", recv_packet[i].dout.sin_port, addr.sin_port);
printf("can't find sockaddr_in!\n");
return 2;
}
}
printf("find sockaddr_in fail!\n");
return -1;
}
static unsigned int get_packet_total(struct sockaddr_in addr)
{
int i;
for (i = 0; i < TRAIN_TOTAL; i++) {
if ((cmp_addr(recv_packet[i].dout, addr) == 0) && (recv_packet[i].state == 1)) {
printf("packet total: %d\n", recv_packet[i].packet_total);
return recv_packet[i].packet_total;
}
}
return -1;
}
/** @defgroup APDU
* @brief decode and encode APDU modules
* @{
*/
/** @defgroup APDU_Defines
* @{
*/
#if 0
#define MY_BIG_ENDIAN 1 /* crd8362 test */
#define COM1 0
#define COM2 1
#define COM_BUF_LEN 30
/**
* @}
*/
static request_function request_handler[CMD_MAX - 1];
static ack_function ack_handler[CMD_MAX - 1];
static error_function error_handler[CMD_MAX - 1];
/** @defgroup APDU_Pubilc_Functions
* @{
*/
/** Set request handler function.
*
*
* @param number [in] .
* @param pFunction [in] .
*/
void set_request_handler(CMD number, request_function p_function)
{
if ((number > 0) && (number < CMD_MAX))
request_handler[number - 1] = p_function;
}
/** Set ack handler function.
*
*
* @param number [in] .
* @param pFunction [in] .
*/
void set_ack_handler(CMD number, request_function p_function)
{
if ((number > 0) && (number < CMD_MAX))
ack_handler[number - 1] = p_function;
}
/** Set error handler function.
*
*
* @param number [in] .
* @param pFunction [in] .
*/
void set_error_handler(CMD number, request_function p_function)
{
if ((number > 0) && (number < CMD_MAX))
error_handler[number - 1] = p_function;
}
#endif
/**
* @}
*/
/** @defgroup APDU_ENCODE
* @{
*/
/** Encode short functions.
*
*
* @param value [in] .
* @param buf [out] .
* @return the number of buf octets, or 0 on failure.
*/
#if 0
static int encode_short_into_buffer(unsigned short value, unsigned char *buf)
{
if (buf == NULL)
return 0;
buf[0] = (unsigned char)((value & 0xff00) >> 8);
buf[1] = (unsigned char)(value & 0x00ff);
return 2;
}
static int encode_int_into_buffer(unsigned int value, unsigned char *buf)
{
if (buf == NULL)
return 0;
buf[0] = (unsigned char) ((value & 0xff000000) >> 24);
buf[1] = (unsigned char) ((value & 0x00ff0000) >> 16);
buf[2] = (unsigned char) ((value & 0x0000ff00) >> 8);
buf[3] = (unsigned char) (value & 0x000000ff);
return 4;
}
#endif
#if 0
static int encode_float_into_buffer(float value, unsigned char *buf)
{
union {
unsigned char byte[4];
float real_value;
} my_data;
if (buf == NULL)
return 0;
my_data.real_value = value;
#if MY_BIG_ENDIAN
buf[0] = my_data.byte[0];
buf[1] = my_data.byte[1];
buf[2] = my_data.byte[2];
buf[3] = my_data.byte[3];
#else
buf[0] = my_data.byte[3];
buf[1] = my_data.byte[2];
buf[2] = my_data.byte[1];
buf[3] = my_data.byte[0];
#endif
return 4;
}
static int encode_channel(channel_data_function value, unsigned char *buf)
{
if (buf == NULL)
return 0;
buf[0] = value.first_channel;
buf[1] = value.channel_num;
return 2;
}
static int encode_uart(uart_data_function value, unsigned char *buf)
{
if (buf == NULL)
return 0;
buf[0] = value.uart_index;
buf[1] = value.board_addr;
encode_short_into_buffer(value.reg_addr, &buf[2]);
buf[4] = value.reg_num;
return 5;
}
static int encode_test_com(com_test_data_function value, unsigned char *buf)
{
if (buf == NULL)
return 0;
buf[0] = value.from_com;
buf[1] = value.to_com;
return 2;
}
static int encode_multiple_float(unsigned short num, float *value, unsigned char *buf)
{
unsigned short i, encode_len = 0;
if ((value == NULL) || (buf == NULL))
return 0;
for (i = 0; i < num; i++) {
encode_len += encode_float_into_buffer(*value, &buf[encode_len]);
value++;
}
return encode_len;
}
#endif
static unsigned short update_crc_ccitt(unsigned short crc, char c)
{
unsigned short q;
int j;
for (j = 0; j < 8; j++) {
q = (crc & 0x0001) ^ (c & 0x0001);
if (q == 0x0001) {
crc = crc >> 1;
crc = crc ^ 0x8408;
} else
crc = crc >> 1;
c = c >> 1;
}
return crc;
}
static unsigned short calc_crc(char *str, int len)
{
unsigned short crc_ccitt/*, low_byte, high_byte*/;
int i;
crc_ccitt = 0xffff;
i = 0;
while (i < len) {
crc_ccitt = update_crc_ccitt(crc_ccitt, str[i]);
i++;
}
return crc_ccitt;
}
/**
* @}
*/
/** Decode
*
*/
/** @defgroup APDU_DECODE
* @{
*/
int decode_short_from_buffer(unsigned short *value, const unsigned char *buf)
{
if (buf == NULL)
return 0;
*value = ((unsigned short)buf[0] << 8) & 0xff00;
*value |= (unsigned short)buf[1] & 0x00ff;
return 2;
}
#if 0
static int decode_int_from_buffer(unsigned int *value, unsigned char *buf)
{
if (buf == NULL)
return 0;
if (value) {
*value = ((unsigned int) ((((unsigned int) buf [0]) << 24) & 0xff000000));
*value |= ((unsigned int) ((((unsigned int) buf [1]) << 16) & 0x00ff0000));
*value |= ((unsigned int) ((((unsigned int) buf [2]) << 8) & 0x0000ff00));
*value |= ((unsigned int) (((unsigned int) buf [3]) & 0x000000ff));
return 4;
} else
return 0;
}
#endif
static int decode_float_from_buffer(float *value, const unsigned char *buf)
{
union {
unsigned char byte[4];
float real_value;
} my_data;
if ((buf == NULL) || (value ==NULL))
return 0;
#if MY_BIG_ENDIAN
my_data.byte[0] = buf[0];
my_data.byte[1] = buf[1];
my_data.byte[2] = buf[2];
my_data.byte[3] = buf[3];
#else
my_data.byte[0] = buf[3];
my_data.byte[1] = buf[2];
my_data.byte[2] = buf[1];
my_data.byte[3] = buf[0];
#endif
*value = my_data.real_value;
return 4;
}
int decode_multiple_float(unsigned short num, float *value, const unsigned char *buf)
{
unsigned short i, encode_len = 0;
if ((value == NULL) || (buf == NULL))
return 0;
for (i = 0; i < num; i++) {
encode_len += decode_float_from_buffer(value, &buf[encode_len]);
value++;
}
return encode_len;
}
static int decode_head(head_s *recv_head, const unsigned char *buf, int len)
{
int value = -1;
if (len < HEAD_LEN)
return value;
if (buf[HEAD_TPYE_INDEX] != APDU_TYPE_REQUEST && buf[HEAD_TPYE_INDEX] != APDU_TYPE_ACK)
return value;
else {
recv_head->type = buf[HEAD_TPYE_INDEX];
value = (int)buf[HEAD_TPYE_INDEX];
}
recv_head->device = (unsigned int)buf[HEAD_DEVICE_INDEX];
recv_head->device = (recv_head->device << 8) | (unsigned int)buf[HEAD_DEVICE_INDEX + 1];
recv_head->device = (recv_head->device << 8) | (unsigned int)buf[HEAD_DEVICE_INDEX + 2];
if (buf[HEAD_NUM_INDEX] != 0x01) {
value = -2;
return value;
} else
recv_head->index = buf[HEAD_NUM_INDEX];
recv_head->message = buf[HEAD_MESSAGE_INDEX];
recv_head->host = buf[HEAD_HOST_ID_INDEX];
decode_short_from_buffer(&recv_head->port, &buf[HEAD_PORT_INDEX]);
if (buf[HEAD_NETGATE_INDEX] != 0x00) {
value = -3;
return value;
} else
recv_head->netgate = buf[HEAD_NETGATE_INDEX];
return value;
}
static int decode_train_num(unsigned char *train_num, unsigned char *buf, int len)
{
int i, value = -1;
if (len < TRAIN_NUM_LEN)
return value;
//memcpy(train_num, buf, TRAIN_NUM_LEN);
printf("train num:");
for(i = 0; i < TRAIN_NUM_LEN; i++) {
train_num[i] = buf[i];
printf("%02x ", buf[i]);
}
printf("\n");
value = TRAIN_NUM_LEN;
return value;
}
static int decode_message_id(unsigned short *message_id, unsigned char *buf, int len)
{
int value = -1;
if (len < MESSAGE_ID_LEN)
return value;
decode_short_from_buffer(message_id, buf);
value = 2;
printf("message id: %02x\n", *message_id);
return value;
}
static int check_message_id(unsigned short message_id)
{
int value = -1;
switch (message_id) {
case CMD_RECV_WORK_MESSAGE:
case CMD_RECV_RECONNECT_MESSAGE:
case CMD_RECV_FEEDBACK_MESSAGE:
case CMD_RECV_CONFIG_MESSAGE:
case CMD_RECV_RECONNECT_FAULT:
case CMD_RECV_RECONNECT_TOTAL:
case CMD_RECV_ID_VERIFICATION:
case CMD_RECV_HEARTBEAT:
value = 1;
break;
default:
break;
}
return value;
}
static int check_crc(const unsigned char *head, const unsigned char *buf, int len)
{
int value = -1;
unsigned short packet_len, temp_crc, recv_crc;
if ((len < PACKET_LENGTH_LEN) || (head == NULL) || (buf == NULL)) {
printf("len is %d\n", len);
return value;
}
decode_short_from_buffer(&packet_len, buf);
if (packet_len > len) {
printf("packet_len is %d\n", packet_len);
return value;
}
//temp_crc = calc_crc(buf, len - TAIL_LEN);
if(len<6)
temp_crc = calc_crc(head, packet_len + HEAD_LEN + TRAIN_NUM_LEN);
else
temp_crc = calc_crc(head, packet_len + HEAD_LEN+ 6 + TRAIN_NUM_LEN);
decode_short_from_buffer(&recv_crc, &buf[packet_len - TAIL_LEN]);
printf("crc buf: %02x %02x\n", buf[packet_len - TAIL_LEN], buf[packet_len - TAIL_LEN + 1]);
printf("packet_len: %d recv crc: %04x calc crc: %04x\n", packet_len, recv_crc, temp_crc);
if (temp_crc == recv_crc)
value = 1;
return value;
// return 1;
}
static int sent_heartbeat(unsigned int device, unsigned char num, int socket_fd, struct sockaddr_in dout)
{
unsigned char temp[17];
unsigned short crc;
temp[0] = 0x30;
temp[1] = (unsigned char)(device >> 16 & 0x000000ff);
temp[2] = (unsigned char)(device >> 8 & 0x000000ff);
temp[3] = (unsigned char)(device & 0x000000ff);
temp[4] = 0x01;
temp[5] = 0x00;
temp[6] = 0x6A;
temp[7] = 0x1A;
temp[8] = 0x85;
temp[9] = 0x00;
temp[10] = 0x10;
temp[11] = 0x00;
temp[12] = 0x00;
temp[13] = 0x05;
temp[14] = num;
crc = calc_crc(temp, 15);
temp[15] = (unsigned char)(crc >> 8);
temp[16] = (unsigned char)(crc & 0x00ff);
return tcp_sent(socket_fd, temp, 17, dout);
}
static void recv_handler(head_s head, unsigned short message_id, const unsigned char *data, int data_len, int socket_fd, struct sockaddr_in dout)
{
switch (message_id) {
case CMD_RECV_WORK_MESSAGE:
break;
case CMD_RECV_RECONNECT_MESSAGE:
break;
case CMD_RECV_FEEDBACK_MESSAGE:
break;
case CMD_RECV_CONFIG_MESSAGE:
break;
case CMD_RECV_RECONNECT_FAULT:
break;
case CMD_RECV_RECONNECT_TOTAL:
break;
case CMD_RECV_ID_VERIFICATION:
break;
case CMD_RECV_HEARTBEAT:
sent_heartbeat(head.device, data[21], socket_fd, dout);
break;
default:
break;
}
}
static char* get_data_time(void)
{
static char now_time[20];
time_t raw_time;
struct tm* l_time;
time(&raw_time);
l_time = localtime(&raw_time);
strftime(now_time, 20, "%Y-%m-%d %H:%M:%S", l_time);
return now_time;
}
static char *dir = "./log";
static char *file_name = "/message.log";
static char* create_train_dir(char *p)
{
static char train_dir[50];
char temp[30];
int i;
if (p == NULL)
return NULL;
memset(train_dir, '\0', sizeof(train_dir));
memset(temp, '\0', sizeof(temp));
//if (NULL == opendir(dir)) {
if (access(dir, F_OK) != 0) {
if (mkdir(dir, 0755) == -1) {
perror("mkdir error");
return NULL;
}
}
for (i = 0; i < TRAIN_NUM_LEN; i++) {
if (p[i] == '\0')
temp[i] = '_';
else
temp[i] = p[i];
}
strcpy(train_dir, dir);
strcat(train_dir, "/");
strcat(train_dir, temp);
if(access(train_dir, F_OK) != 0) {
if(mkdir(train_dir, 0755) == -1) {
perror("mkdir error");
return NULL;
}
}
printf("%s\n", temp);
printf("%s\n", train_dir);
return train_dir;
}
static int write_head(FILE *p, head_s h_data)
{
if (p == NULL)
return -1;
fprintf(p, "head: type=0x%02x device=%d index=%d message=%d host=%d port=%d netgate=%d\n", h_data.type, h_data.device, h_data.index, h_data.message, h_data.host, h_data.port, h_data.netgate);
return 1;
}
static int write_message_id(FILE *p, unsigned short message_id)
{
if (p == NULL)
return -1;
fprintf(p, "message id: 0x%04x\n", message_id);
return 1;
}
#if 0
static void disp_temperature(FILE *p, unsigned char data)
{
char temp;
temp = data;
if (data == 0x81)
fprintf(p, "error ");
else if (data == 0x82)
fprintf(p, "none ");
else
fprintf(p, "%d ", (int)temp);
}
#endif
static int write_work_body(FILE *p, unsigned char *data, int len)
{
int i;
char t_num[T_NUM_LEN + 1];
char temp;
//bearing_s bearing_data[7];
//alarm_data_s alarm_data;
//status_s status_data;
gps_s gps_data;
char now_time[20];
//char train_info[TRAIN_INFO_LIN + 1];
time_t raw_time;
unsigned int recv_time;
struct tm* l_time;
int c_len;
if ((p == NULL) || (data == NULL))
return -1;
if (len < WORK_LEN) {
fprintf(p, "data error, len is: %d less than %d\n", len, WORK_LEN);
return -2;
}
memset(t_num, '\0', sizeof(t_num));
memcpy(t_num, &data[2], T_NUM_LEN);
fprintf(p, "train num:%s\n", t_num);
temp = data[2 + T_NUM_LEN + RESERVE_LEN];
fprintf(p, "temperature_env1=%d ", (int)temp);
temp = data[2 + T_NUM_LEN + RESERVE_LEN + 1];
fprintf(p, "temperature_env2=%d ", (int)temp);
temp = data[2 + T_NUM_LEN + RESERVE_LEN + 2];
fprintf(p, "temperature_main_generator=%d ", (int)temp);
temp = data[2 + T_NUM_LEN + RESERVE_LEN + 3];
fprintf(p, "temperature_fan1=%d ", (int)temp);
temp = data[2 + T_NUM_LEN + RESERVE_LEN + 4];
fprintf(p, "temperature_fan2=%d\n", (int)temp);
c_len = 2 + T_NUM_LEN + RESERVE_LEN + OTHER_TEMPERATURE_LEN;
for (i = 0; i < 6; i++) {
fprintf(p, "bearing %d data:\n", i + 1);
temp = data[c_len + i * 6 + 0];
fprintf(p, "temperature_1=%d ", (int)temp);
temp = data[c_len + i * 6 + 1];
fprintf(p, "temperature_2=%d ", (int)temp);
temp = data[c_len + i * 6 + 2];
fprintf(p, "temperature_3=%d ", (int)temp);
temp = data[c_len + i * 6 + 3];
fprintf(p, "temperature_4=%d ", (int)temp);
temp = data[c_len + i * 6 + 4];
fprintf(p, "temperature_5=%d ", (int)temp);
temp = data[c_len + i * 6 + 5];
fprintf(p, "temperature_6=%d\n", (int)temp);
}
c_len = 2 + T_NUM_LEN + RESERVE_LEN + OTHER_TEMPERATURE_LEN + TEMPERATURE_LEN;
temp = data[c_len];
fprintf(p, "alarm data:\n");
fprintf(p, "temperature_max=%d ", (int)temp);
fprintf(p, "max_num=%d ", (int)data[c_len + 1]);
fprintf(p, "alarm_identifier=%d ", (int)data[c_len + 2]);
fprintf(p, "current_alarm_num=%d ", (int)data[c_len + 3]);
recv_time = (unsigned int)data[c_len + 4];
recv_time = (recv_time << 8) | (unsigned int)data[c_len + 5];
recv_time = (recv_time << 8) | (unsigned int)data[c_len + 6];
recv_time = (recv_time << 8) | (unsigned int)data[c_len + 7];
raw_time = (time_t)recv_time;
l_time = localtime(&raw_time);
strftime(now_time, 20, "%Y-%m-%d %H:%M:%S", l_time);
fprintf(p, "alarm_time=%s\n", now_time);
fprintf(p, "alarm_code=%d ", (int)data[c_len + 8]);
temp = data[c_len + 9];
fprintf(p, "alarm_test_temperature=%d ", (int)temp);
temp = data[c_len + 10];
fprintf(p, "alarm_temperature_ref=%d\n", (int)temp);
c_len = 2 + T_NUM_LEN + RESERVE_LEN + OTHER_TEMPERATURE_LEN + TEMPERATURE_LEN + ALARM_LEN + OTHER_LEN;
fprintf(p, "gps_data:\n");
decode_float_from_buffer(&gps_data.east, &data[c_len]);
fprintf(p, "gps_east=%f ", gps_data.east);
fprintf(p, "gps_east_minute=%d ", (int)data[c_len + 4]);
decode_float_from_buffer(&gps_data.north, &data[c_len + 5]);
fprintf(p, "gps_north=%f ", gps_data.north);
fprintf(p, "gps_north_minute=%d ", (int)data[c_len + 9]);
gps_data.speed = data[c_len + 10];
fprintf(p, "gps_speed=%d\n", (int)gps_data.speed);
fprintf(p, "\n");
return 1;
}
static int write_reconnect_message(FILE *p, unsigned char *data, int len)
{
int i;
unsigned short temp;
short temp_t;
unsigned int recv_time;
char now_time[20];
time_t raw_time;
struct tm* l_time;
gps_s gps_data;
int c_len;
if ((p == NULL) || (data == NULL))
return -1;
if (len < RECONNECT_MESSAGE_MAX) {
fprintf(p, "data error, len is: %d less than %d\n", len, RECONNECT_MESSAGE_MAX);
return -2;
}
fprintf(p, "reconnect message data:\n");
for (i = 0; i < RECONNECT_MESSAGE; i = i + 2) {
if (i < 82) {
temp = (unsigned short)data[2 + i];
temp = (temp << 8) | (unsigned short)data[2 + i + 1];
fprintf(p, "(%d~%d)=%d ", i+1, i+2, (int)temp);
} else if (i < 96) {
if (i == 82)
fprintf(p, "\n");
temp = (unsigned short)data[2 + i];
temp = (temp << 8) | (unsigned short)data[2 + i + 1];
temp_t = temp;
fprintf(p, "(%d~%d)=%d ", i+1, i+2, (int)temp_t);
} else if (i < 98){
fprintf(p, "\n");
fprintf(p, "(%d)=0x%02x ", i+1, data[2 + i]);
fprintf(p, "(%d)=0x%02x\n", i+2, data[2 + i + 1]);
} else if (i < 104) {
temp = (unsigned short)data[2 + i];
temp = (temp << 8) | (unsigned short)data[2 + i + 1];
fprintf(p, "(%d~%d)=%d ", i+1, i+2, (int)temp);
} else if (i == 104) {
fprintf(p, "\n");
recv_time = (unsigned int)data[2 + i];
recv_time = (recv_time << 8) | (unsigned int)data[2 + i + 1];
recv_time = (recv_time << 8) | (unsigned int)data[2 + i + 2];
recv_time = (recv_time << 8) | (unsigned int)data[2 + i + 3];
raw_time = (time_t)recv_time;
l_time = localtime(&raw_time);
strftime(now_time, 20, "%Y-%m-%d %H:%M:%S", l_time);
fprintf(p, "time=%s\n", now_time);
break;
}
}
c_len = 2 + RECONNECT_MESSAGE;
fprintf(p, "gps_data:\n");
decode_float_from_buffer(&gps_data.east, &data[c_len]);
fprintf(p, "gps_east=%f ", gps_data.east);
fprintf(p, "gps_east_minute=%d ", (int)data[c_len + 4]);
decode_float_from_buffer(&gps_data.north, &data[c_len + 5]);
fprintf(p, "gps_north=%f ", gps_data.north);
fprintf(p, "gps_north_minute=%d ", (int)data[c_len + 9]);
gps_data.speed = data[c_len + 10];
fprintf(p, "gps_speed=%d\n", (int)gps_data.speed);
fprintf(p, "\n");
return 1;
}
static int write_reconnect_fault(FILE *p, unsigned char *data, int len)
{
int i;
//unsigned short train_num, ticket_num;
unsigned int recv_time;
char now_time[20];
time_t raw_time;
struct tm* l_time;
int c_len;
gps_s gps_data;
if ((p == NULL) || (data == NULL))
return -1;
if (len < RECONNECT_FAULT_MAX) {
fprintf(p, "data error, len is: %d less than %d\n", len, RECONNECT_FAULT_MAX);
return -2;
}
fprintf(p, "reconnect fault data:\n");
//train_num = (unsigned short)data[2];
//train_num = (train_num << 8) | (unsigned short)data[3];
//fprintf(p, "train_num=%d ", (int)train_num);
//ticket_num = (unsigned short)data[4];
//ticket_num = (ticket_num << 8) | (unsigned short)data[5];
//fprintf(p, "ticket_num=%d\n", (int)ticket_num);
for (i = 0; i < RECONNECT_FAULT; i++) {
if (i < 45) {
if (data[2 + i] == 0xff)
fprintf(p, "(%d)=fault ", i+1);
else if (data[2 + i] == 0)
fprintf(p, "(%d)=normal ", i+1);
else
fprintf(p, "(%d)=error ", i+1);
} else if (i == 45) {
fprintf(p, "\n");
recv_time = (unsigned int)data[2 + i];
recv_time = (recv_time << 8) | (unsigned int)data[2 + i + 1];
recv_time = (recv_time << 8) | (unsigned int)data[2 + i + 2];
recv_time = (recv_time << 8) | (unsigned int)data[2 + i + 3];
raw_time = (time_t)recv_time;
l_time = localtime(&raw_time);
strftime(now_time, 20, "%Y-%m-%d %H:%M:%S", l_time);
fprintf(p, "time=%s\n", now_time);
break;
}
}
c_len = 2 + RECONNECT_FAULT;
fprintf(p, "gps_data:\n");
decode_float_from_buffer(&gps_data.east, &data[c_len]);
fprintf(p, "gps_east=%f ", gps_data.east);
fprintf(p, "gps_east_minute=%d ", (int)data[c_len + 4]);
decode_float_from_buffer(&gps_data.north, &data[c_len + 5]);
fprintf(p, "gps_north=%f ", gps_data.north);
fprintf(p, "gps_north_minute=%d ", (int)data[c_len + 9]);
gps_data.speed = data[c_len + 10];
fprintf(p, "gps_speed=%d\n", (int)gps_data.speed);
fprintf(p, "\n");
return 1;
}
static int write_reconnect_total(FILE *p, unsigned char *data, int len)
{
int i;
unsigned short temp;
unsigned int recv_time, temp_32;
char now_time[20];
time_t raw_time;
struct tm* l_time;
int c_len;
gps_s gps_data;
if ((p == NULL) || (data == NULL))
return -1;
if (len < RECONNECT_TOTAL_MAX) {
fprintf(p, "data error, len is: %d less than %d\n", len, RECONNECT_TOTAL_MAX);
return -2;
}
fprintf(p, "reconnect total data:\n");
fprintf(p, "protocol version=%.2f\n", (float)data[2] / 10.0);
fprintf(p, "software slave=%d ", (int)data[2 + 1]);
fprintf(p, "software master=%d\n", (int)data[2 + 2]);
temp = (unsigned short)data[2 + 3];
temp = (temp << 8) | (unsigned short)data[2 + 4];
fprintf(p, "train_num=%d ", (int)temp);
temp = (unsigned short)data[2 + 5];
temp = (temp << 8) | (unsigned short)data[2 + 6];
fprintf(p, "train_type=0x%02x\n", (int)temp);
if (data[2 + 7] == 0x01)
fprintf(p, "A or B=A\n");
else if (data[2 + 7] == 0x02)
fprintf(p, "A or B=B\n");
else if (data[2 + 7] == 0xff)
fprintf(p, "A or B=none\n");
else
fprintf(p, "A or B=error\n");
for (i = 0; i < 8; i++) {
temp_32 = (unsigned int)data[2 + 8 + i * 4];
temp_32 = (temp_32 << 8) | (unsigned int)data[2 + 9 + i * 4];
temp_32 = (temp_32 << 8) | (unsigned int)data[2 + 10 + i * 4];
temp_32 = (temp_32 << 8) | (unsigned int)data[2 + 11 + i * 4];
fprintf(p, "(%d~%d)=%d ", 9 + i * 4, 12 + i * 4, temp_32);
}
fprintf(p, "\n");
recv_time = (unsigned int)data[2 + 45];
recv_time = (recv_time << 8) | (unsigned int)data[2 + 46];
recv_time = (recv_time << 8) | (unsigned int)data[2 + 47];
recv_time = (recv_time << 8) | (unsigned int)data[2 + 48];
raw_time = (time_t)recv_time;
l_time = localtime(&raw_time);
strftime(now_time, 20, "%Y-%m-%d %H:%M:%S", l_time);
fprintf(p, "time=%s\n", now_time);
c_len = 2 + RECONNECT_TOTAL;
fprintf(p, "gps_data:\n");
decode_float_from_buffer(&gps_data.east, &data[c_len]);
fprintf(p, "gps_east=%f ", gps_data.east);
fprintf(p, "gps_east_minute=%d ", (int)data[c_len + 4]);
decode_float_from_buffer(&gps_data.north, &data[c_len + 5]);
fprintf(p, "gps_north=%f ", gps_data.north);
fprintf(p, "gps_north_minute=%d ", (int)data[c_len + 9]);
gps_data.speed = data[c_len + 10];
fprintf(p, "gps_speed=%d\n", (int)gps_data.speed);
fprintf(p, "\n");
return 1;
}
#define NUM_PER_LINE 20
static int write_body(FILE *p, unsigned char *data, int len, unsigned short message_id)
{
int i, j, line;
// data++;
// data++;
unsigned char * data1;
data1=data+0x02;
if ((p == NULL) || (data == NULL))
return -1;
switch (message_id) {
case CMD_RECV_WORK_MESSAGE:
return write_work_body(p, data, len);
//break;
case CMD_RECV_RECONNECT_MESSAGE:
return write_reconnect_message(p, data, len);
//break;
case CMD_RECV_FEEDBACK_MESSAGE:
break;
case CMD_RECV_CONFIG_MESSAGE:
break;
case CMD_RECV_RECONNECT_FAULT:
return write_reconnect_fault(p, data, len);
//break;
case CMD_RECV_RECONNECT_TOTAL:
return write_reconnect_total(p, data, len);
//break;
case CMD_RECV_ID_VERIFICATION:
break;
case CMD_RECV_HEARTBEAT:
break;
default:
printf("error!! unrecognized message id!\n");
return -2;
//break;
}
#if 1
// if (len <= 0) {
// return 2;
// }
fprintf(p, "data1:");
line = len / NUM_PER_LINE;
if ((len % NUM_PER_LINE) != 0)
line += 1;
for (j = 0; j < line - 1; j++) {
for (i = 0; i < NUM_PER_LINE; i++)
fprintf(p, " 0x%02x", data[i]);
fprintf(p, "\n");
}
for (j = j * NUM_PER_LINE; j < len; j++)
fprintf(p, " 0x%02x", data[j]);
fprintf(p, "\n");
return 1;
#endif
}
static int write_message_to_file(char *train_dir, char *file_name, head_s h_data, unsigned short message_id, unsigned char *data, int len, unsigned int total)
{
char file_path[100];
FILE *file_p;
static unsigned int packet_num;
static unsigned int current_packet_num;
static unsigned int pre_packet_num;
current_packet_num = total;
if (pre_packet_num<=current_packet_num)
{
pre_packet_num=current_packet_num;
}
else{
current_packet_num=(++pre_packet_num);
pre_packet_num =current_packet_num;
}
if (train_dir == NULL || file_name == NULL || data == NULL)
return -1;
memset(file_path, '\0', sizeof(file_path));
strcpy(file_path, train_dir);
strcat(file_path, file_name);
/*if (packet_num == 1) {
if((file_p = fopen(file_path, "w+")) == NULL) {
perror("fopen file error");
return -1;
}
} else */
//{
if((file_p = fopen(file_path, "a+")) == NULL) {
perror("fopen file error");
return -1;
}
// }
packet_num= current_packet_num;
//fprintf(file_p,"current_packet_num:",current_packet_num);
fprintf(file_p, "------------------ pakcet %d -------------------\n", packet_num);
fprintf(file_p, "%s\n", get_data_time());
if (write_head(file_p, h_data) < 0) {
fclose(file_p);
return -2;
}
if (write_message_id(file_p, message_id) < 0) {
fclose(file_p);
return -3;
}
if (write_body(file_p, data, len, message_id) < 0) {
fclose(file_p);
return -4;
}
fprintf(file_p, "\n");
fclose(file_p);
return 1;
}
/**
* @}
*/
void decode_apdu_handler(int socket_fd, unsigned char *apdu, int apdu_len, struct sockaddr_in dout)
{
unsigned char type;
head_s recv_head;
unsigned char train_num[TRAIN_NUM_LEN];
unsigned short message_id;
unsigned char data[PACKET_MAX_LEN];
unsigned char data1[PACKET_MAX_LEN];
unsigned char encrypted_data[PACKET_MAX_LEN];
unsigned char decrypted_data[PACKET_MAX_LEN];
unsigned char handle_data[PACKET_MAX_LEN];
int decode_len, temp, data_len,decode_len1;
int recv_crc = 0;
char *p;
int i= 0;
//-----------------------------//////////////////////////////////////////////////////////////
unsigned char str[1024];
unsigned char dst[1024];
int data_fd;
int data_fd1;
int data_fd2;
int res;
int bytes_write=0;
int bytes_read = 0;
//------------------------------//////////////////////////////////////////////////////////////
static unsigned char trans_mode;
//------------------------------/////////////////////////////////////////////////////////////
if (apdu && (socket_fd != -1) && apdu_len >= PACKET_MIN_LEN) {
type = (unsigned char)decode_head(&recv_head, apdu, apdu_len);
//if(apdu_len<35)
decode_len= HEAD_LEN;
// else
// decode_len= HEAD_LEN+ID_CARD;
//-------------------------------------///////////////////////////////////////////////////////////////////////////////////////
memcpy(data1, apdu, apdu_len);
data_fd1 = open("apdu_data.txt",O_WRONLY|O_APPEND|O_CREAT,0644);
for(i=0;i<data_len;i++)
{
str[2*i] = data1[i] >>4;
str[2*i+1] = data1[i] & 0xf;
}
for(i=0;i<2*data_len;i++)
{
sprintf(&dst[i],"%X/n",str[i]);
printf("%c ",dst[i]);
}
res = write(data_fd1, dst, 600);
bytes_write += res;
close(data_fd1);
printf("Process %d finished, %d bytes read\n", getpid(), bytes_write);
//---------------------------------------/////////////////////////////////////////////////////////////////////////////////////////
switch (type)
{
case APDU_TYPE_REQUEST:
temp = decode_train_num(train_num, &apdu[decode_len], apdu_len - decode_len);
if (temp > 0)
//printf("temp");
decode_len+= temp;
//decode_len=decode_len1+ID_CARD;
//printf("temp:%s\n",temp);
else {
printf("decode train num error!\n");
return;
}
if(apdu_len>35)
decode_len= HEAD_LEN+13;
//decode_len= HEAD_LEN;
temp = decode_message_id(&message_id, &apdu[decode_len], apdu_len - decode_len);
if (temp > 0)
decode_len += temp;
else {
printf("decode message id error!\n");
return;
}
if (check_message_id(message_id) < 0) {
printf("decode message id out of range!\n");
return;
}
if (check_crc(apdu, &apdu[decode_len], apdu_len - decode_len) < 0) {
printf("crc error!\n");
return;
}
recv_crc = (apdu[apdu_len-2] <<8 ) | apdu[apdu_len-1];
if(calc_crc(apdu,apdu_len-2) != recv_crc)
{
printf("crc error!%d\n");
//return;
}
//----------------------------------------------------------------------------//--------------------------------------------------------------------------
/*
printf("start\r\n ");
for(i=0;i<apdu_len;i++)
printf("%02x ",apdu[i]);
printf("\r\nend \r\n");
*/
//----------------------------------------------------------------------//------------------------------------------------------------------------------------
if(apdu_len<28)
{
memcpy(data, apdu, apdu_len);
trans_mode=(data[21]>>6);
//-------------------------------------------------------------------------------------------------------////////////////////////////////////////////////////////////////////////
memcpy(data1, apdu, apdu_len);
//--------------------------------------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////////////
printf("\r\n heart is :\r\n");
for(i=0;i<apdu_len;i++)
printf("%02x ",apdu[i]);
printf("\r\n");
}
else
{
//trans_mode =0x10;
memcpy(encrypted_data,&apdu[17],6);
memcpy(&encrypted_data[6],&apdu[27],apdu_len-23);
////////////////////////----------------------------------------------------------------------------------------------------------------------------
memcpy(data,apdu,17);
memcpy(data,apdu,4);
memcpy(&data[17],&apdu[23],4);
/////////////////////////-------------------------------------------------------------------------------------------------------/////////////////////////////////////////////////////////////////////////////
memcpy(data1,apdu,17);
memcpy(data1,apdu,4);
memcpy(&data1[17],&apdu[23],4);
///////////////////////------------------------------------------------------------------------------------------------------/////////////////////////////////////////////////////////////////////////////////
//memcpy(data1,&apdu[23],4);
//memcpy(data1,&apdu[0],6);
// mqtt_num++;
// memcpy(&data1[0],&mqtt_num,4);
// memcpy(&data1[4],&message_count,4);
memcpy(&data1[0],&ip_addr,4);
memcpy(&data1[4],&trans_mode,1);
memcpy(&data1[5],&port,2);
memcpy(&data1[7],&apdu[23],4);
/////////////////////--------------------------------------------------------------//////////////////////////////////////////
data_fd2 = open("encrypted_data.txt",O_WRONLY|O_APPEND|O_CREAT,0644);
for(i=0;i<data_len;i++)
{
str[2*i] = encrypted_data[i] >>4;
str[2*i+1] = encrypted_data[i] & 0xf;
}
for(i=0;i<2*data_len;i++)
{
sprintf(&dst[i],"%X/n",str[i]);
printf("%c ",dst[i]);
}
res = write(data_fd2, dst,600);
bytes_write += res;
close(data_fd2);
printf("Process %d finished, %d bytes read\n", getpid(), bytes_write);
//////////////////////////////////--------------------------------------------------------------------------////////////////////////////////////////////
printf("\r\n脢媒鸥脻鲁鈧睹埪B�%d\r\n",apdu_len-23);
if(server_decrypt(encrypted_data,decrypted_data,apdu_len-23) == 0)
{
for(i=0;i<apdu_len-29;i++)
printf("%02x ",decrypted_data[i]);
}
//-//////////////////-------------------------------------------------------------------------------------------------////////////////////
memcpy(&data[0],&apdu[23],4);
memcpy(&data[4],decrypted_data,apdu_len-29);
memcpy(&data1[11],decrypted_data,apdu_len-29);
/////////////////////////////---------------------------------------------------------------------//////////////////////////////
/*
printf("data-------------------------------------------------------------\r\n ");
for(i=0;i<apdu_len-25;i++)
printf("%02X",data[i]);
printf("\r\nend \r\n");
*/
////////////////////////////////////------------------------MQTT-----------------------------------------------------------///////////////////////////////////////////////////////////////////////
MQTT_topic_data(data,apdu_len - 25);
//////////////////////////////////----------------------------------------------------------------------------------------////////////////////////////////////////////////////////////////////////
data_fd = open("DataFormFIFO.log",O_WRONLY|O_APPEND|O_CREAT,0644);
for(i=0;i<data_len;i++)
{
str[2*i] = data[i] >>4;
str[2*i+1] = data[i] & 0xf;
}
for(i=0;i<2*data_len;i++)
{
sprintf(&dst[i],"%X/n",str[i]);
printf("%c ",dst[i]);
}
res = write(data_fd, dst,600);
bytes_write += res;
close(data_fd);
printf("Process %d finished, %d bytes read\n", getpid(), bytes_write);
//------------------------------------------------------------------------------////////////////////////////////////////////
/*
printf("kaishi:\r\n");
for(i=0;i<apdu_len-29;i++)
printf("%02x ",data[i]);
printf("\r\n");
*/
}
//memcpy(data, &apdu[decode_len1], apdu_len - decode_len1);
data_len = apdu_len - decode_len;
//---------------------------------------------------------------------------------------------------/////////////////////////////////////////////////////
recv_handler(recv_head, message_id, data, data_len,socket_fd, dout);
set_packet_total(dout);
if ((p = create_train_dir(train_num)) == NULL)
printf("create dir error!\n");
if (write_message_to_file(p, file_name, recv_head, message_id, data, data_len, get_packet_total(dout)) < 0)
printf("write file error!\n");
//-------------------------------------------------------------------------------------------------/////////////////////////////////////////////////////
break;
case APDU_TYPE_ACK:
printf("recv ack data!!!!!!\n");
break;
default:
printf("decode type-(%d) error!\n", type);
break;
}
} else
printf("decode apdu error!\n");
}
void udp_thread(void)
{
unsigned char data[PACKET_MAX_LEN];
int data_len = 0;
int socket_fd = -1;
//char test_flag = 0;
struct sockaddr_in dout;
unsigned short i = 0;
bool quiet = false;
// unsigned char sent_data[PACKET_MAX_LEN];
//int j;
#if 0
unsigned short xxx;
unsigned char zzz[74] = {
0x30, 0x6b, 0x00, 0xa8, 0x01, 0x00, 0x6c, 0x23, 0x31, 0x00, 0x30,
0x30, 0x31, 0x32, 0x33, 0x34, 0x2c, 0x10, 0x00,
0x00, 0x05, 0x80, 0xd9, 0x00};
xxx = calc_crc(zzz, 22);
printf("crc %04x\n", xxx);
#endif
/* Initialize sent_apdu_function pointer */
//init_handler();
/* TCP socket server setup */
tcp_set_port(htons(0x1A85));//6789
printf("set udp port is 6789!\n");
//operator_login();
if (tcp_init())
printf("udp server socket opened!\n");
int_packet();
//------------------------------------------------//////////////////////////////////////////////////////////////////////
int k=0;
char *username="thit";
char *password="qwe!@#";
char *host = "192.168.9.12";
int port = 1883;
int keepalive = 60;
bool clean_session = true;
mosquitto_lib_init();
mosq = mosquitto_new("track01", clean_session, NULL);
if(!mosq){
fprintf(stderr, "Error: Out of memory.\n");
/*return 0;*/
}
mosquitto_log_callback_set(mosq, my_log_callback);
//*mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_message_callback_set(mosq, my_message_callback);
mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
//---------------------------------------------------------------------------------------------------------------
//----return :------------------
// x= mosquitto_username_pw_set(mosq, username, password);
//printf("----------------------------------------------data= %d\n",x);
//printf("username= %s\n",username);
//---------------------------------------------------------------------------------------------------------------
if(username && mosquitto_username_pw_set(mosq, username, password)){
printf("---------------------------------UUUUUUU-------------------------------------------------------------username= %s\n",(*mosq).username);
}
else
{ //设置用户名,密码
if(!quiet){
fprintf(stderr, "Error: Problem setting username and password.\n");
}
// return 1;
}
if(mosquitto_connect(mosq,host, port, keepalive))
{
fprintf(", %d", k);
/* return 1;*/
}
//}
//-----------------------------------------------////////////////////////////////////////////////////////////////////////
while (1) {
/*
if (heart_num <255)
{ heart_num++;
}
else
{
heart_num=0;
}
*/
/* Initialize test board */
//init_test_handler();
/* TCP process */
socket_fd = tcp_get_socket();
#if 0
decode_apdu_handler(socket_fd, zzz, 74, dout);
return;
#endif
#if 0
data_len = 32;
printf("------------------start---------------------\n");
if (decrypt(data, handle_data, data_len) == 0) {
printf("decrypt len = %d\n", data_len);
printf("unsigned char decrypt_data = {\n");
for (j = 0; j < data_len; j++) {
if (j != data_len - 1)
printf("0x%02x, ", handle_data[j]);
else
printf("0x%02x};\n", handle_data[j]);
}
} else
printf("decrypt error!!!\n");
for (j = 0; j < 13; j++) {
sent_data[j] = j;
}
apdu_sent(socket_fd, sent_data, 13, dout);
printf("--------------------------------------------\n");
return;
#endif
// printf ("s2\n");
data_len = tcp_receive(socket_fd, data, PACKET_MAX_LEN, 10, &dout);
if (data_len > 0) {
printf("RX: ");
for (i = 0; i < data_len; i++) //test
printf("%02x ", data[i]);
printf("\n");
decode_apdu_handler(socket_fd, data, data_len, dout);
//apdu_sent(socket_fd, handle_data, data_len, dout);
data_len = 0;
}
}
////////////////////////////////////////////////////-------------------------------MQTT---------------------------------------///////////////////////////////////////////////////////////////////
mosquitto_loop_forever(mosq, -1, 1);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
////////////////////////////////////////////////////-----------------------------------------------------------------------////////////////////////////////////////////////////////////////////////
return;
}
/*
void java_udp_init(void)
{
pthread_t thread_id;
pthread_create(&thread_id, NULL, (void *)udp_thread, NULL);
}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2012 THIT *****END OF FILE****/
MQTT-C-UDP_PUB的更多相关文章
- 海鑫智圣:物联网漫谈之MQTT协议
什么是MQTT协议 MQTT(消息队列遥测传输协议)是IBM在1999年专门针对物联网等应用场景来制订的轻量级双向消息传输协议,它主要是为了解决物联网上使用到的设备的互相通信的问题,以及这些设备与后端 ...
- 几个MQTT的知识点
开始正文前需要感谢一下网友“小龙”和emqtt.io群里的网友们的帮助,本人刚刚开始使用MQTT有很多不懂的地方,在emqtt.io群里询问解决方法的时候,“小龙”给我详细的讲解了一些MQTT的知识点 ...
- 基于MQTT协议进行应用开发
官方协议有句如下的话来形容MQTT的设计思想: "It is designed for connections with remote locations where a "sma ...
- MQTT(三)-----连接与心跳
MQTT协议笔记之连接和心跳 - 推酷 http://www.tuicool.com/articles/AFvmee 互联网推送服务原理:长连接+心跳机制(MQTT协议) - clh604的专栏 - ...
- Mosquitto搭建Android推送服务(一)MQTT简介
总体概要: MQTT系列文章分为4部分 1.MQTT简介 2.mosquitto服务器搭建 3.编写Mosquitto的可视化工具 4.使用Mosquitto完成Android推送服务 文章钢要: 对 ...
- 【开源】MQTT推送服务器——zer0MqttServer(Java编写)
目录 说明 功能 如何使用 参考帮助 说明 重要的放前面:V1.0版本是一个非常基础的版本,除了完整的MQTT协议实现外,其他功能什么都没做. MQTT 协议是 IBM 开发的即时通讯协议,相对于 I ...
- TCP/IP, WebSocket 和 MQTT
按照OSI网络分层模型,IP是网络层协议,TCP是传输层协议,而HTTP和MQTT是应用层的协议.在这三者之间, TCP是HTTP和MQTT底层的协议.大家对HTTP很熟悉,这里简要介绍下MQTT.M ...
- 云巴:基于MQTT协议的实时通信编程模型
概要 有人常问,云巴实时通信系统到底提供了一种怎样的服务,与其他提供推送或 IM 服务的厂商有何本质区别.其实,从技术角度分析,云巴与其它同类厂商都是面向开发者的通信服务,宏观的编程模型都是大同小异, ...
- MQTT 消息 发布 订阅
当连接向一个mqtt服务器时,clientId必须是唯一的.设置一样,导致client.setCallback总是走到 connectionLost回调.报connection reset.调查一天才 ...
- MQTT开发笔记之《MQTT Server》
MQTT SERVER 性能测试报告 : http://w3yyb.sinaapp.com/archives/1601各个MQTT SERVER功能列表: http://blog.lenix.xyz/ ...
随机推荐
- WebGL 利用FBO完成立方体贴图。
这篇主要记录WebGL的一些基本要点,顺便也学习下如何使用FBO与环境贴图.先看下效果图(需要支持WebGL,Chrome,火狐,IE11). 主要实现过程如下,先用FBO输出当前环境在立方体纹理中, ...
- 【转】【Nginx】Nginx 入门教程 + 常用配置解析
== Nginx介绍和安装 == Nginx是一个自由.开源.高性能及轻量级的HTTP服务器及反转代理服务器, 其性能与IMAP/POP3代理服务器相当.Nginx以其高性能.稳定.功能丰富.配置简单 ...
- 【转】【Python】Python多进程与多线程
1.1 multiprocessing multiprocessing是多进程模块,多进程提供了任务并发性,能充分利用多核处理器.避免了GIL(全局解释锁)对资源的影响. 有以下常用类: 类 描述 P ...
- JDBC选择数据库实例
在本教程将演示如何在JDBC应用程序中选择一个数据库. 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模式中创建数据库. 要执行以下示例,需要用实际用户名和密码替换这 ...
- form中的button按钮在IE11中自动提交表单问题导致弹出框关闭之后表单被重置
最近几天,测试系统,遇到一个兼容性问题,form中有一个button按钮,没有指定type类型,点击按钮弹出框选择值之后回填给form上的一个单行文本框,在IE6.IE7.IE8.IE9.IE10中测 ...
- 安装tensorflow出现问题的解法
在ubuntu14.04用pip安装tensorflow-gpu 安装1.3.0遇到问题 1.安装tensorflow出现Cannot uninstall 'six'.问题的解法 https://bl ...
- 边框颜色为 tintColor 的 UIButton
创建一个 UIButton 的子类,重写其方法: - (void)drawRect:(CGRect)rect { [[self layer] setCornerRadius:CORNER_RADIUS ...
- iOS :UIPickerView reloadAllComponets not work
编辑信息页面用了很多选择栏,大部分都用 UIPickerView 来实现.在切换数据显示的时候, UIPickerView 不更新数据,不得其解.Google 无解,原因在于无法描述自己的问题,想想应 ...
- SOAP消息头的处理
SOAP消息头的处理 WebService学习总结(四)——调用第三方提供的webService服务 SOAP中 RPC/ENC 为啥被抛弃
- [原创]解决jQuery.live在mobile safari(iphone / ipad / ipod)绑定失败的问题
解决方案: 给要使用live绑定事件的元素,添加“cursor:pointer”样式即可! 如: a,input,td{cursor:pointer;} 原文链接:http://bugs.jquery ...