void init_frame(frame * frame) { frame->type = 0; frame->window = 0; frame->seq = 0; init_payload(frame->payload); frame->crc = 0; }
void ping_init(protocol_t protocol, uint8_t channr, radio_address_t addr){ protocol_id = protocol; r_address = addr; cc1100_set_packet_handler(protocol, ping_handler); cc1100_set_channel(channr); cc1100_set_address(r_address); init_payload(); }
void create_ack_frame(uint8_t seq, frame* frame) { frame->type = 2; frame->window = MAX_WINDOW_SIZE - 1; frame->seq = get_seq(seq); frame->length = 0; init_payload(frame->payload); frame->crc = compute_crc(*frame); }
void create_data_frame(uint8_t seq, char* data, uint16_t len, frame* frame) { frame->type = 1; frame->window = 0; frame->seq = get_seq(seq); frame->length = len; init_payload(frame->payload); memcpy(frame->payload, data, frame->length + 1); frame->crc = compute_crc(*frame); }
/* test Function */ int send_Push_Message(int argc, char *argv) { //int err; /* Phone specific Payload message as well as hex formated device token */ const char *deviceTokenHex = NULL; if(argc == 1) { deviceTokenHex = "ba458798b1996bef05e7b0cff7b554b82aaf7f61d5e062cab3d4ee5fed417bb2"; } else { deviceTokenHex = argv; } if(strlen(deviceTokenHex) < 64 || strlen(deviceTokenHex) > 70) { log_error("Device Token is to short or to long. Length without spaces should be 64 chars...,the devicetoken is %s\n", deviceTokenHex); return -1; } Payload *payload = (Payload*)malloc(sizeof(Payload)); init_payload(payload); // This is the message the user gets once the Notification arrives payload->message = "Message to print out"; // This is the red numbered badge that appears over the Icon payload->badgeNumber = 1; // This is the Caption of the Action key on the Dialog that appears payload->actionKeyCaption = "Caption of the second Button"; // These are two dictionary key-value pairs with user-content payload->dictKey[0] = "Key1"; payload->dictValue[0] = "Value1"; payload->dictKey[1] = "Key2"; payload->dictValue[1] = "Value2"; /* Send the payload to the phone */ log_error("Sending APN to Device with UDID: %s\n", deviceTokenHex); send_remote_notification(deviceTokenHex, payload); return 0; }
/* MAIN Function */ int main(int argc, char *argv[]) { /* Phone specific Payload message as well as hex formated device token */ const char* deviceTokenHex = NULL; if(argc == 1) { deviceTokenHex = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; } else { deviceTokenHex = argv[1]; } if(strlen(deviceTokenHex) < 64 || strlen(deviceTokenHex) > 70) { printf("Device Token is to short or to long. Length without spaces should be 64 chars...\n"); printf("%s\n", deviceTokenHex); exit(1); } Payload *payload = (Payload*)malloc(sizeof(Payload)); init_payload(payload); // This is the message the user gets once the Notification arrives payload->message = "Message to print out"; // This is the red numbered badge that appears over the Icon payload->badgeNumber = 1; // This is the Caption of the Action key on the Dialog that appears payload->actionKeyCaption = "Caption of the second Button"; // These are two dictionary key-value pairs with user-content payload->dictKey[0] = "Key1"; payload->dictValue[0] = "Value1"; payload->dictKey[1] = "Key2"; payload->dictValue[1] = "Value2"; /* Send the payload to the phone */ printf("Sending APN to Device with UDID: %s\n", deviceTokenHex); send_remote_notification(deviceTokenHex, payload); return 0; }
int main(int argc, char *argv[], char *env[]) { int c; char *redis_server = "127.0.0.1"; int redis_port = 6379; char * redis_queue = "apple_apn"; bool deamon_mode = false; int pid, worker_pid, worker_pid_wait; redisContext *redis; redisReply *reply; cJSON *json; char *deviceToken, *message, *caption; int badge; /* process arguments */ while ((c = getopt(argc, argv, "s:p:q:c:t:dh")) != -1) { switch (c) { case 's': redis_server = strdup(optarg); //malloc + strcpy break; case 'p': redis_port = atoi(optarg); break; case 'q': redis_queue = strdup(optarg); break; case 'd': deamon_mode = true; break; case 'h': default: show_help(); return 1; } } //以守护进程运行 if(deamon_mode == true) { /* Fork off the parent process */ pid = fork(); if (pid < 0) { exit(0); } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { exit(1); } } /* 派生子进程(工作进程) */ worker_pid = fork(); //如果是父进程 if(worker_pid > 0) { /* 处理父进程接收到的kill信号 */ /* 忽略Broken Pipe信号 */ signal(SIGPIPE, SIG_IGN); /* 处理kill信号 */ signal (SIGINT, kill_signal_master); signal (SIGKILL, kill_signal_master); signal (SIGQUIT, kill_signal_master); signal (SIGTERM, kill_signal_master); signal (SIGHUP, kill_signal_master); /* 处理段错误信号 */ signal(SIGSEGV, kill_signal_master); /* 如果子进程终止,则重新派生新的子进程 */ while (1) { worker_pid_wait = wait(NULL); if (worker_pid_wait < 0) { continue; } usleep(100000); worker_pid = fork(); /*成功生成子进程*/ if (worker_pid == 0) { break; } } } /* ---------------子进程内容------------------- */ /* 忽略Broken Pipe信号 */ signal(SIGPIPE, SIG_IGN); /* 处理kill信号 */ signal (SIGINT, kill_signal_worker); signal (SIGKILL, kill_signal_worker); signal (SIGQUIT, kill_signal_worker); signal (SIGTERM, kill_signal_worker); signal (SIGHUP, kill_signal_worker); /* 处理段错误信号 */ signal(SIGSEGV, kill_signal_worker); /* working */ struct timeval timeout = { 1, 500000 }; // 1.5 seconds redis = redisConnectWithTimeout(redis_server, redis_port, timeout); reply = redisCommand(redis, "SUBSCRIBE apple_apn"); freeReplyObject(reply); while(redisGetReply(redis, (void**)&reply) == REDIS_OK) { printf("content:%s\n", reply->element[2]->str); json = cJSON_Parse(reply->element[2]->str); if (!json) { printf("Error before: [%s]\n",cJSON_GetErrorPtr()); continue; } deviceToken = cJSON_GetObjectItem(json,"deviceToken")->valuestring; message = cJSON_GetObjectItem(json,"message")->valuestring; caption = cJSON_GetObjectItem(json,"caption")->valuestring; badge = cJSON_GetObjectItem(json,"badge")->valueint; if(deviceToken == NULL || message == NULL || caption == NULL || badge == 0 || strlen(deviceToken) != 64 ) { printf("send params error:<deviceToken = %s ,message = %s ,caption = %s ,badge = %d>\n", deviceToken, message, caption, badge); continue; } Payload *payload = (Payload*)malloc(sizeof(Payload)); init_payload(payload); //deviceToken = "83d4071e8c9a0afff108c2fa431a569cd71298d863da2462a555994ca3aac53a"; payload->message = message; payload->badgeNumber = badge; payload->actionKeyCaption = caption; payload->dictKey[0] = NULL; payload->dictValue[0] = NULL; printf("Sending APN to Device with UDID: %s\n", deviceToken); send_remote_notification(deviceToken, payload); } while(1) { sleep(2); /* 出错的话尝试重连 */ if (redis->err) { redis = redisConnectWithTimeout(redis_server, redis_port, timeout); printf("redis Server[%s] connection error: %s\n", redis_server, redis->errstr); continue; kill_signal_master(SIGQUIT); }else{ exit(0); } //process(redis, redis_queue, loop_time); } }