void GSM_PrepareSMS(float tmp1, float tmp2) { char data [] = {"Temperatura: \nWilgotnosc: "}; uint8_t t1 = (uint8_t)tmp1; uint8_t t2 = (uint8_t)tmp2; uint8_t t3,t4; t3=t1%10; t1=t1/10; data[13] = (char)(48+t1); data[14] = (char)(48+t3); t4=t2%10; t2=t2/10; data[28] = (char)(48+t2); data[29] = (char)(48+t4); GSM_SendSMS("604218929", data); }
/** * @name action_send_messages: */ int action_send_messages(gammu_state_t **sp, int argc, char *argv[]) { int rv = 0; char **argp = &argv[1]; if (argc <= 2) { print_usage_error(U_ERR_ARGS_MISSING); return 1; } if (argc % 2 != 1) { print_usage_error(U_ERR_ARGS_ODD); return 2; } /* Lazy initialization of libgammu */ gammu_state_t *s = gammu_create_if_necessary(sp); if (!s) { print_operation_error(OP_ERR_INIT); rv = 3; goto cleanup; } /* Allocate */ smsc_t *smsc = allocate(sizeof(*smsc)); multimessage_t *sms = allocate(sizeof(*sms)); multimessage_info_t *info = allocate(sizeof(*info)); /* Find SMSC number */ smsc->Location = 1; if ((s->err = GSM_GetSMSC(s->sm, smsc)) != ERR_NONE) { print_operation_error(OP_ERR_SMSC); rv = 4; goto cleanup_sms; } transmit_status_t status; initialize_transmit_status(&status); GSM_SetSendSMSStatusCallback( s->sm, _message_transmit_callback, &status ); boolean_t is_start = TRUE; unsigned int message_index = 0; printf("["); /* For each message... */ while (*argp != NULL) { GSM_ClearMultiPartSMSInfo(info); GSM_Debug_Info *debug = GSM_GetGlobalDebug(); /* Copy/convert destination phone number */ char *sms_destination_number = convert_utf8_utf16be(*argp++, FALSE); if (!sms_destination_number) { status.err = "Invalid UTF-8 sequence in destination number"; goto cleanup_end; } string_info_t nsi; utf16be_string_info(sms_destination_number, &nsi); /* Check size of phone number: We'll be decoding this in to a fixed-sized buffer. */ if (nsi.units >= GSM_MAX_NUMBER_LENGTH) { status.err = "Phone number is too long"; goto cleanup_transmit_status; } /* Missing message text: This shouldn't happen since we check `argc` above, but I'm leaving this here in case we refactor later. */ if (*argp == NULL) { status.err = "No message body provided"; goto cleanup_transmit_status; } /* UTF-8 message content */ char *sms_message = *argp++; /* Convert message from UTF-8 to UTF-16-BE: Every symbol is two bytes long; the string is then terminated by a single 2-byte UTF-16 null character. */ char *sms_message_utf16be = convert_utf8_utf16be(sms_message, FALSE); if (!sms_message_utf16be) { status.err = "Invalid UTF-8 sequence"; goto cleanup_transmit_status; } /* Prepare message info structure: This information is used to encode the possibly-multipart SMS. */ info->Class = 1; info->EntriesNum = 1; info->Entries[0].ID = SMS_ConcatenatedTextLong; info->Entries[0].Buffer = (uint8_t *) sms_message_utf16be; info->UnicodeCoding = !utf16be_is_gsm_string(sms_message_utf16be); if ((s->err = GSM_EncodeMultiPartSMS(debug, info, sms)) != ERR_NONE) { status.err = "Failed to encode message"; goto cleanup_sms_text; } status.parts_sent = 0; status.parts_total = sms->Number; /* For each SMS part... */ for (unsigned int i = 0; i < sms->Number; i++) { status.finished = FALSE; status.message_part_index = i; sms->SMS[i].PDU = SMS_Submit; /* Copy destination phone number: This is a fixed-size buffer; size was already checked above. */ CopyUnicodeString(sms->SMS[i].SMSC.Number, smsc->Number); CopyUnicodeString( sms->SMS[i].Number, (unsigned char *) sms_destination_number ); /* Transmit a single message part */ if ((s->err = GSM_SendSMS(s->sm, &sms->SMS[i])) != ERR_NONE) { status.parts[i].err = "Message transmission failed"; continue; } for (;;) { /* Wait for reply */ GSM_ReadDevice(s->sm, TRUE); if (status.finished) { break; } } if (!status.parts[i].transmitted) { status.parts[i].err = "Message delivery failed"; continue; } status.parts_sent++; } cleanup_sms_text: status.message_index = ++message_index; free(sms_message_utf16be); cleanup_transmit_status: print_json_transmit_status(s, sms, &status, is_start); free(sms_destination_number); cleanup_end: is_start = FALSE; } cleanup_sms: free(sms); free(smsc); free(info); printf("]\n"); cleanup: return rv; }
static void cgi_process_each(GSM_StateMachine *s, GSM_SMSMessage*sms) { int child_in[2]; int child_out[2]; int pid; int ret; int offset; int status; child_in[0] = child_in[1] = child_out[0] = child_out[1] = -1; /* invalid fd */ DecodeUnicode(sms->Text, buffer); smprintf(s, CGI_ENGINE "<< [%s]\n", buffer); /* ----------------------------------------------------- now open the pipes */ if (pipe(child_in)) { smprintf(s, CGI_ENGINE "Unable to open to pipe: %s\n", strerror(errno)); goto error; } if (pipe(child_out)) { smprintf(s, CGI_ENGINE "Unable to open from pipe: %s\n", strerror(errno)); goto error; } /* ------------------------- Block SIGHUP during the fork - prevents a race */ //sigfillset(&signal_set); //pthread_sigmask(SIG_BLOCK, &signal_set, &old_set); //signal(SIGCHLD, cgi_child_end); /* ----------------------------------------------------------- fork the cgi */ pid = fork(); if(pid < 0) { smprintf(s, CGI_ENGINE "Could not fork: %s\n", strerror(errno)); // pthread_sigmask(SIG_SETMASK, &old_set, NULL); goto error; } if(!pid) { /* -------------------------------------------------- child process */ /* ------------------------------------ move stdout to child_out[1] */ close(STDOUT_FILENO); dup2(child_out[1], STDOUT_FILENO); close(child_out[1]); close(child_out[0]); /* close unused read end */ /* -------------------------------------- move stdin to child_in[0] */ close(STDIN_FILENO); dup2(child_in[0], STDIN_FILENO); close(child_in[0]); close(child_in[1]); /* close unused write end */ cgi_child(s); } /* --------------------------------------------------------- parent process */ close(child_out[1]); /* close unused write end */ close(child_in[0]); /* close unused read end */ smprintf(s, CGI_ENGINE "Launched CGI script\n"); /* ----------------------------------------------------------- send headers */ DecodeUnicode(sms->Number, buffer2); cgi_write_header(s, child_in[1], "SMS_FROM", buffer2); DecodeUnicode(sms->Name, buffer2); cgi_write_header(s, child_in[1], "SMS_NAME", buffer2); cgi_write_header(s, child_in[1], "SMS_TIME", OSDate(sms->DateTime)); /* -------------------------------------------- End headers with empty line */ cgi_write_helper(s, child_in[1], "\r\n", sizeof("\r\n") - 1); /* ----------------------------------------------- now we write the command */ cgi_write_helper(s, child_in[1], buffer, strlen(buffer)); close(child_in[1]); /* send EOF */ /* ------------------------------------------------------------ now we read */ smprintf(s, CGI_ENGINE ">>======== CGI Response ==========\n"); buffer[0] = '\0'; offset = 0; while((ret = read(child_out[0], buffer + offset, 1)) > 0) { offset += ret; *(buffer+offset) = '\0'; } smprintf(s, CGI_ENGINE "%s", buffer); smprintf(s, "\n>>================================\n"); do { /* ------------------------------------------------- wait for child to exit */ if((ret = waitpid(pid, &status, WNOHANG)) == -1) { smprintf(s, CGI_ENGINE " waitpid failed :(\n"); goto error; } if(!ret) { smprintf(s, CGI_ENGINE " Child is not dead yet ..\n"); } if(!WIFEXITED(status)) { if(WIFSIGNALED(status)) { smprintf(s, CGI_ENGINE "killed by signal %d\n", WTERMSIG(status)); } else if (WIFSTOPPED(status)) { smprintf(s, CGI_ENGINE "stopped by signal %d\n", WSTOPSIG(status)); } else if (WIFCONTINUED(status)) { smprintf(s, CGI_ENGINE "continued\n"); } } } while(!WIFEXITED(status) && !WIFSIGNALED(status)); smprintf(s, CGI_ENGINE " Child process exited\n"); if(buffer[0] != '\0') { /* ----------------------------------------------- prepare response */ memset(&smsSendBuffer, 0, sizeof(smsSendBuffer)); /* reset memory */ GSM_SetDefaultSMSData(&smsSendBuffer); smsSendBuffer.Location = 1; smsSendBuffer.Class = 1; smsSendBuffer.PDU = SMS_Submit; smsSendBuffer.Coding = SMS_Coding_Default_No_Compression; CopyUnicodeString(smsSendBuffer.Number, sms->Number); EncodeUnicode(smsSendBuffer.Text, buffer, strlen(buffer)); /* -------------------------------------------------- send response */ error = GSM_SendSMS(s, &smsSendBuffer); } error: close(child_in[0]); close(child_in[1]); close(child_out[0]); close(child_out[1]); GSM_DeleteSMS(s, sms); return; }