int main() { SSL_load_error_strings(); ERR_load_BIO_strings(); OpenSSL_add_all_algorithms(); BIO *bio = BIO_new_connect("gateway.sandbox.push.apple.com:2195"); if (bio == NULL) { ERR_print_errors_fp(stdout); exit(1); } if (BIO_do_connect(bio) <= 0) { ERR_print_errors_fp(stdout); exit(1); } char buf[MAXBUF]; char *json = "{\"aps\":{\"badge\":123}}"; sendPayload(bio, token, json, strlen(json)); int ret = BIO_read(bio, buf, MAXBUF); if (ret <= 0) { printf("BIO_read return 0\n"); } BIO_free_all(bio); return 0; }
int main() { SSL_load_error_strings(); ERR_load_BIO_strings(); OpenSSL_add_all_algorithms(); SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method()); if (ctx == NULL) { printf("SSL_CTX_new err func:%s\n reaseon:%s", ERR_func_error_string(ERR_get_error()), ERR_reason_error_string(ERR_get_error())); exit(1); } //加载可信任证书库 if (0 == SSL_CTX_load_verify_locations(ctx, "./push_cer.pem", NULL)) { printf("err func:%s\n reaseon:%s", ERR_func_error_string(ERR_get_error()), ERR_reason_error_string(ERR_get_error())); ERR_print_errors_fp(stdout); exit(1); } //set BIO BIO *bio = BIO_new_ssl_connect(ctx); if (bio == NULL) { printf("err func:%s\n", ERR_func_error_string(ERR_get_error())); ERR_print_errors_fp(stdout); exit(1); } SSL *ssl; BIO_get_ssl(bio, &ssl); SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); //open safe connect BIO_set_conn_hostname(bio, "gateway.sandbox.push.apple.com:2195"); //verify connect ok if (BIO_do_connect(bio) <= 0) { ERR_print_errors_fp(stdout); exit(1); } if (SSL_get_verify_result(ssl) != X509_V_OK) { printf("SSL_get_verify_result not success\n"); } char buf[MAXBUF]; char *json = "{\"aps\":{\"badge\":123}}"; sendPayload(bio, token, json, strlen(json)); int ret = BIO_read(bio, buf, MAXBUF); if (ret <= 0) { printf("BIO_read return 0\n"); } SSL_CTX_free(ctx); BIO_free_all(bio); return 0; }
TransmitResult internalSendPacket(const uint8_t *buff, const size_t &length, const uint32_t maxTimeoutUs, bool requestAck) { irq = false; const size_t toSendLength = MIN(length, RFM73_MAX_PACKET_LEN); sendPayload(buff, toSendLength, requestAck ? 1 : 0); TransmitResult result; uint32_t i = 0; uint8_t status = 0; bool readStatus = true; while (!irq && i++ < maxTimeoutUs) { delayMicroseconds(1); if (i >= 10 && i % 5 == 0) { if (checkStatusForMissingIRQ(status)) { readStatus = false; break; } } } if (readStatus) { status = readRegVal(RFM73_REG_STATUS); } if (status & RFM73_IRQ_STATUS_TX_DS) { result.status = Success; result.bytesSent = toSendLength; } else if (status & RFM73_IRQ_STATUS_MAX_RT) { result.status = MaxRT; result.bytesSent = 0; } else if (status & RFM73_IRQ_STATUS_TX_FULL) { result.status = FifoFull; result.bytesSent = 0; } if (i >= maxTimeoutUs) { result.status = Unknown; result.bytesSent = 0; } // Reset error flags writeRegVal(RFM73_CMD_WRITE_REG | RFM73_REG_STATUS, status); return result; }
TransmitResult_t RFM75_Transmit_bytes(const uint8_t *buff, const uint32_t *length, const uint32_t maxTimeoutUs, bool requestAck) { TransmitResult_t result; const uint32_t toSendLength = *length; uint32_t i=0; uint8_t status = 0; bool readStatus = true; sendPayload(buff, toSendLength, (uint8_t)requestAck); rxtx_interrupt = 0; while((rxtx_interrupt == 0) && (i < maxTimeoutUs)){ _delay_us(20); if(checkStatusForMissingIRQ(&status)){ readStatus=false; break; } i++; } rxtx_interrupt = 0; /* Clear the status interrupt */ writeRegVal(RFM7x_CMD_WRITE_REG | RFM7x_REG_STATUS, status); if (readStatus){ status = readRegVal(RFM7x_REG_STATUS); } if (status & RFM7x_IRQ_STATUS_TX_DS){ result.status = SUCCESS; result.bytesSent = toSendLength; } else if (status & RFM7x_IRQ_STATUS_MAX_RT){ result.status = MAXRT; result.bytesSent = 0; } else if (status & RFM7x_IRQ_STATUS_TX_FULL){ result.status = FIFOFULL; result.bytesSent = 0; } if (i >= maxTimeoutUs){ result.status = UNKNOWN; result.bytesSent = 0; } return result; }
int sendFile(int socket, char *fileName ) { FILE *fp = fopen(fileName, "r"); int rc; DEBUG(("\nSendFile:Opened file %s\n", fileName)); char fileBuf[1000]={0}; //File buffer fileTransferPayload *ftpBuf = (fileTransferPayload *) malloc(1000); int seqNo = 0; if (fp == NULL) { DEBUG(("\nSendFile:File %s Not found\n", fileName)); return RC_FILE_NOT_FOUND; } DEBUG(("\nSendFile:\n")); strcpy(ftpBuf->fileName, fileName); ftpBuf->statusFlag |= FTP_REQUEST; do { memset(ftpBuf->filePayload,0,790); rc = fread(fileBuf,sizeof(char), 790, fp); DEBUG(("\nSendfile: RC = %d\n", rc)); if (rc < 0) { return RC_FILE_NOT_FOUND; } if (!seqNo) { ftpBuf->statusFlag |= FTP_START; } if (feof(fp)) { ftpBuf->statusFlag |= FTP_STOP; } ftpBuf->statusFlag = htons(ftpBuf->statusFlag); memcpy(ftpBuf->filePayload, fileBuf, rc); rc = sendPayload(socket, MSG_FILE_TRANSFER, ftpBuf, sizeof(fileTransferPayload) + rc); if (rc != RC_SUCCESS) { DEBUG(("\nCould not send script file\n")); free(ftpBuf); return rc; } ftpBuf->statusFlag = 0; seqNo++; }while(!feof(fp)); free(ftpBuf); fclose(fp); DEBUG(("\nSendFile:Just before returning\n")); return RC_SUCCESS; }
/********************************************************* ** This is the function used to send request for topology ** ** ** ** Arguments: ** socket : socket on which the payload was received. ** numOfNodes : Num of nodes in topology. ** buf : buffer having IP addresses of nodes. ***********************************************************/ void sendTopologyJoinRequest(int socket) { int size = (sizeof(topologyRequestPayload)); time_t t; long timestamp = time(&t); printf("\n\nSize = %d",size); //LOG(INFO, "Sending Join Request to Admission %s:", "Contact"); printf("\nSending Join Request \n"); topologyRequestPayload *payloadBuf = calloc(1,sizeof(topologyRequestPayload)); payloadBuf->flags = ADD_NODE_REQUEST; payloadBuf->timestamp = timestamp; memcpy(payloadBuf->ipAddr, myIP, 16); printf("\nSending Join Request %s\n", myIP); sendPayload(socket, MSG_TOPOLOGY_REQUEST, payloadBuf, size); free(payloadBuf); }
/********************************************************* ** This is function used to send request for topology ** request ** ** ** Arguments: ** socket : socket on which the payload was received. ** numOfNodes : Num of nodes in topology. ** buf : buffer having IP addresses of nodes. ***********************************************************/ void sendTopologyResponse(int socket, int numOfNodes, char *buf) { int size = (sizeof(addDeleteNodePayload) + (numOfNodes * ID_SIZE)); addDeleteNodePayload *payloadBuf = calloc(1,size); payloadBuf->numOfNodes = numOfNodes; printf("\n Sending here 1\n"); payloadBuf->flags = ADD_PAYLOAD | COMPLETE_PAYLOAD; payloadBuf->ttl = 0; //No need to propogate memcpy(payloadBuf->ID, buf, numOfNodes * ID_SIZE); printf("\n Sending here 2\n"); //getchar(); printf("\n Sending here 3\n"); sendPayload(socket, MSG_ADD_DELETE_NODE, payloadBuf, size); printf("\n Sending response \n"); free(payloadBuf); }
/** * boolean send(char*) : function to enqueue a packet for transmission * returns: true, false **/ void RFM70::send(uint8_t* s, uint8_t len) { //switch to tx mode cliTxDs(); setMode(MODE_PTX); if (txTimeout()) { flushTxFIFO(); cliTimeout(); } sendPayload(s, len); //wait for packet to be sent _delay_us(SEND_DELAY); //switch to rx mode cliRxDr(); setMode(MODE_PRX); }
/** * boolean send(char*) : function to enqueue a packet for transmission * returns: true, false **/ void RFM70::send(uint8_t* s, uint8_t len) { // Switch to tx mode pthread_mutex_lock(&g_mutex); cliTxDs(); setMode(MODE_PTX); if (txTimeout()) { flushTxFIFO(); cliTimeout(); } sendPayload(s, len); //wait for packet to be sent usleep(SEND_DELAY); //switch to rx mode cliRxDr(); setMode(MODE_PRX); pthread_mutex_unlock(&g_mutex); }
uint8_t RFM70::sendPayload(uint8_t * payload, uint8_t len) { sendPayload(payload, len, NO_ACK); }
uint8_t RFM70::sendAckPayload(uint8_t * payload, uint8_t len) { sendPayload(payload, len, -1); }