Advertisement
Guest User

dooralarm.c

a guest
Aug 27th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.22 KB | None | 0 0
  1. // dooralarm.c
  2. // works with motion daemon
  3. // sends email with attached snapshot from usb webcam
  4. // when door is opened
  5. //
  6. // connect reed switch to gpio 7
  7. // raspberry pi pin 7
  8.  
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <stdlib.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <syslog.h>
  15. #include <time.h>
  16. #include <errno.h>
  17. #include <netdb.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <resolv.h>
  22. #include <string.h>
  23. #include <bcm2835.h>
  24. #include <stdio.h>
  25.  
  26. #define PIN RPI_GPIO_P1_07
  27. uint8_t LASTSTATE = 1;
  28.  
  29. char *getdatetime();
  30. char *getoutput1_base64();
  31. long int getoutput1size();
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.     logit ("Calling daemonize()");
  36.     daemonize();
  37.  
  38.     int i;
  39.     const char *email[2];
  40.     email[0] = "noc@<DOMAIN>";
  41.  
  42.     if (!bcm2835_init())
  43.         return 1;
  44.     // Set RPI pin P1-15 to be an input
  45.     bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
  46.     //  with a pullup
  47.     bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
  48.  
  49.     while (1)
  50.     {
  51.         // Read some data
  52.         uint8_t value = bcm2835_gpio_lev(PIN);
  53.         if ( value == 1 && LASTSTATE == 0 ){
  54.             char *timestamp;
  55.             timestamp = getdatetime();
  56.             long int output1size = getoutput1size();
  57.             int count = 0;
  58.  
  59.             logit ("Door 01 OPENED.");
  60.             //sleep(15);
  61.             //system("/usr/bin/fswebcam -d /dev/video0 -i 0 -r 960x720 --timestamp '%Y-%m-%d %H:%M:%S (%Z)' --scale 640x480 --rotate 180  - | /usr/bin/base64 > /tmp/output1.jpg");
  62.  
  63.             //while ( output1size < 367001 && count <= 20 ){
  64.             while ( output1size < 3625001 && count <= 20 ){
  65.                 sleep ( 3 );
  66.                 output1size = getoutput1size();
  67.                 count++;
  68.             }
  69.  
  70.             for ( i=0; i<1; i++){
  71.                 sendemail( email[i], timestamp );
  72.             }
  73.         }
  74.         if ( value == 0 && LASTSTATE == 1 ){
  75.             logit ("Door 01 CLOSED.");
  76.         }
  77.         LASTSTATE = value;
  78.         delay(500);
  79.     }
  80.     bcm2835_close();
  81.     return 0;
  82. }
  83.  
  84. int sendemail(char *email, char *timestamp){
  85.     int a;
  86.     int socket_desc;
  87.     char sendto[128];
  88.     char atsymbol[] = "@";
  89.     char *mxs[10];
  90.     char mailserver[128];
  91.     char *domain;
  92.     char *output1 = getoutput1_base64();
  93.     struct sockaddr_in server;
  94.  
  95.     // copy email address
  96.     strcpy(sendto,email);
  97.  
  98.     // parse domain portion of email
  99.     domain = strstr(sendto, atsymbol);
  100.     memmove(&domain[0], &domain[1], strlen(domain) - 0);
  101.  
  102.     // get mx record
  103.     a=resolvmx("newbeecolo.com", mxs,10);
  104.     struct hostent* mxserver = gethostbyname (mxs[a-1]);
  105.     strcpy(mailserver,inet_ntoa( *( struct in_addr*)( mxserver -> h_addr_list[0])));
  106.  
  107.     //Create socket
  108.     socket_desc = socket(AF_INET , SOCK_STREAM , 0);
  109.     if (socket_desc == -1)
  110.     {
  111.         printf("Could not create socket");
  112.     }
  113.     server.sin_addr.s_addr = inet_addr(mailserver);
  114.     server.sin_family = AF_INET;
  115.     server.sin_port = htons( 25 );
  116.  
  117.     //Connect to remote server
  118.     if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
  119.     {
  120.         puts("connect error");
  121.         return 1;
  122.     }
  123.      
  124.     char *message , server_reply[2000];
  125.     char *message2, *message3, *message4, *message5, *message6, *message7;
  126.     char *mime1, *mime2, *mime3, *mime4, *mime5, *mime6, *mime7;
  127.  
  128.     char *emailbody;
  129.     char emailbody2[256];
  130.  
  131.     message = "helo lab01.newbeecolo.com\r\n";
  132.     send(socket_desc , message , strlen(message) , 0);
  133.     //if( recv(socket_desc, server_reply , 2000 , 0) < 0) { //puts("recv failed"); }
  134.     //puts("Reply received\n");
  135.     //puts(server_reply);
  136.     message2 = "mail from:<alarmpi@HOSTNAME>\r\n";
  137.     send(socket_desc , message2 , strlen(message2) , 0);
  138.     message3 = "rcpt to:";
  139.     send(socket_desc , message3 , strlen(message3) , 0);
  140.     send(socket_desc , email , strlen(email) , 0);
  141.     send(socket_desc , "\r\n" , strlen("\r\n") , 0);
  142.     message4 = "data\r\n";
  143.     send(socket_desc , message4 , strlen(message4) , 0);
  144.  
  145.     message5 = "To:";
  146.     send(socket_desc , message5 , strlen(message5) , 0);
  147.     send(socket_desc , email , strlen(email) , 0);
  148.     send(socket_desc , "\r\n" , strlen("\r\n") , 0);
  149.     message6 = "Subject: Door 01 opened.\r\n";
  150.     send(socket_desc , message6 , strlen(message6) , 0);
  151.  
  152.     mime1 = "Content-Type: multipart/mixed; boundary=\"3.1415926535897932384626\"\r\n";
  153.     send(socket_desc , mime1 , strlen(mime1) , 0);
  154.     mime2 = "MIME_Version: 1.0\r\n";
  155.     send(socket_desc , mime2 , strlen(mime2) , 0);
  156.     send(socket_desc , "\r\n" , strlen("\r\n") , 0);
  157.     mime3 = "--3.1415926535897932384626\r\n";
  158.     send(socket_desc , mime3 , strlen(mime3) , 0);
  159.     mime4 = "Content-Type: text/html; charset=ISO-8859-1\r\n";
  160.     send(socket_desc , mime4 , strlen(mime4) , 0);
  161.     send(socket_desc , "\r\n" , strlen("\r\n") , 0);
  162.     send(socket_desc , "\r\n" , strlen("\r\n") , 0);
  163.  
  164.     mime3 = "--3.1415926535897932384626\r\n";
  165.     send(socket_desc , mime3 , strlen(mime3) , 0);
  166.     //mime5 = "Content-Type: application/octet-stream; name=\"door01.mp4\"\r\n";
  167.     //mime5 = "Content-Type: video/x-msvideo; name=\"door01.avi\"\r\n";
  168.     //mime5 = "Content-Type: video/H264; name=\"door01.avi\"\r\n";
  169.     mime5 = "Content-Type: video/mpeg; name=\"door01.mp4\"\r\n";
  170.     send(socket_desc , mime5 , strlen(mime5) , 0);
  171.     mime6 = "Content-Transfer-Encoding: base64\r\n";
  172.     send(socket_desc , mime6 , strlen(mime6) , 0);
  173.     mime7 = "Content-Disposition: attachment; filename=\"door01.mp4\"\r\n";
  174.     send(socket_desc , mime7 , strlen(mime7) , 0);
  175.     send(socket_desc , "\r\n" , strlen("\r\n") , 0);
  176.     send(socket_desc , output1, strlen(output1), 0);
  177.     send(socket_desc , "\r\n" , strlen("\r\n") , 0);
  178.  
  179.     mime3 = "--3.1415926535897932384626--\r\n";
  180.     send(socket_desc , mime3 , strlen(mime3) , 0);
  181.  
  182.     message7 = ".\r\n";
  183.     send(socket_desc , message7 , strlen(message7) , 0);
  184.      
  185.     return 1;
  186. }
  187.  
  188. int resolvmx(const char *name, char **mxs, int limit) {
  189.     unsigned char response[NS_PACKETSZ];  /* big enough, right? */
  190.     ns_msg handle;
  191.     ns_rr rr;
  192.     int mx_index, ns_index, len;
  193.     char dispbuf[4096];
  194.  
  195.     if ((len = res_search(name, C_IN, T_MX, response, sizeof(response))) < 0) {
  196.         /* WARN: res_search failed */
  197.         return -1;
  198.     }
  199.     if (ns_initparse(response, len, &handle) < 0) {
  200.         /* WARN: ns_initparse failed */
  201.         return 0;
  202.     }
  203.     len = ns_msg_count(handle, ns_s_an);
  204.     if (len < 0)
  205.         return 0;
  206.     for (mx_index = 0, ns_index = 0;
  207.             mx_index < limit && ns_index < len;
  208.             ns_index++) {
  209.         if (ns_parserr(&handle, ns_s_an, ns_index, &rr)) {
  210.             /* WARN: ns_parserr failed */
  211.             continue;
  212.         }
  213.         ns_sprintrr (&handle, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
  214.         if (ns_rr_class(rr) == ns_c_in && ns_rr_type(rr) == ns_t_mx) {
  215.             char mxname[MAXDNAME];
  216.             dn_expand(ns_msg_base(handle), ns_msg_base(handle) + ns_msg_size(handle), ns_rr_rdata(rr) + NS_INT16SZ, mxname, sizeof(mxname));
  217.             mxs[mx_index++] = strdup(mxname);
  218.         }
  219.     }
  220.     return mx_index;
  221. }
  222.  
  223. int daemonize() {
  224.     pid_t pid, sid;
  225.     /* Fork off the parent process */
  226.     pid = fork();
  227.     if (pid < 0) {
  228.             exit(EXIT_FAILURE);
  229.     }
  230.     /* If we got a good PID, then we can exit the parent process. */
  231.     if (pid > 0) {
  232.             exit(EXIT_SUCCESS);
  233.     }
  234.     /* Change the file mode mask */
  235.     umask(0);
  236.     /* Open any logs here */
  237.     /* Create a new SID for the child process */
  238.     sid = setsid();
  239.     if (sid < 0) {
  240.             /* Log the failure */
  241.             exit(EXIT_FAILURE);
  242.     }
  243.     /* Change the current working directory */
  244.     if ((chdir("/")) < 0) {
  245.             /* Log the failure */
  246.             exit(EXIT_FAILURE);
  247.     }
  248.     /* Close out the standard file descriptors */
  249.     close(STDIN_FILENO);
  250.     close(STDOUT_FILENO);
  251.     close(STDERR_FILENO);
  252.     writepid( pid );
  253.     return 1;
  254. }
  255.  
  256. int logit (char *msg) {
  257.     char logfile[] = "/tmp/dooralarm.log";
  258.     char *timestamp;
  259.     char logmsg[128];
  260.     timestamp = getdatetime();
  261.  
  262.     strcpy(logmsg, timestamp);
  263.     strcat(logmsg, " ");
  264.     strcat(logmsg, msg);
  265.     strcat(logmsg, "\n");
  266.  
  267.     FILE *fp;
  268.     fp = fopen(logfile, "a");
  269.     fprintf(fp, logmsg );
  270.     fclose(fp);
  271. }
  272.  
  273. char *getdatetime() {
  274.   char buffer[128];
  275.   time_t curtime;
  276.   struct tm *loctime;
  277.  
  278.   /* Get the current time.  */
  279.   curtime = time (NULL);
  280.   /* Convert it to local time representation.  */
  281.   loctime = localtime (&curtime);
  282.   strftime (buffer, 128, "%Y-%m-%d %H:%M:%S", loctime);
  283.  
  284.   return buffer;
  285. }
  286.  
  287. int writepid(){
  288.     char pidfile[] = "/var/run/dooralarm.pid";
  289.     char str[128];
  290.     sprintf(str,"%d\n",getpid());
  291.     FILE *fp;
  292.     fp = fopen(pidfile, "w");
  293.     fprintf(fp, str );
  294.     fclose(fp);
  295. }
  296.  
  297. char *getoutput1_base64(){
  298.     //system("/usr/bin/cat /srv/http/cam1/output1.avi | /usr/bin/base64 > /tmp/output1_base64.avi");
  299.     //FILE *f = fopen("/tmp/output1.jpg", "rb");
  300.     //FILE *f = fopen("/srv/http/cam1/output1.avi", "rb");
  301.     //FILE *f = fopen("/tmp/output1_base64.avi", "rb");
  302.     //system("/usr/bin/mencoder /srv/http/cam1/output1.avi -idx -ovc lavc -lavcopts vcodec=mjpeg:mbd=1:vbitrate=1800 -o - | /usr/bin/base64 > /tmp/door01.mp4");
  303.     //system("/usr/bin/mencoder /srv/http/cam1/output1.avi -idx -ovc x264 -x264encopts pass=1:turbo:bitrate=900:bframes=1 -vf scale=-10:-1,harddup -o - | /usr/bin/base64 > /tmp/door01.avi");
  304.     system("/usr/bin/cat /srv/http/cam1/output1.avi | /usr/bin/base64 > /tmp/door01.avi");
  305.     FILE *f = fopen("/tmp/door01.avi", "rb");
  306.     fseek(f, 0, SEEK_END);
  307.     long fsize = ftell(f);
  308.     fseek(f, 0, SEEK_SET);
  309.  
  310.     char *string = malloc(fsize + 1);
  311.     fread(string, fsize, 1, f);
  312.     fclose(f);
  313.  
  314.     string[fsize] = 0;
  315.     return string;
  316. }
  317.  
  318. long getoutput1size(){
  319.     FILE *f = fopen("/srv/http/cam1/output1.avi", "rb");
  320.     fseek(f, 0, SEEK_END);
  321.     long fsize = ftell(f);
  322.     //fseek(f, 0, SEEK_SET);
  323.  
  324.     //char *string = malloc(fsize + 1);
  325.     //fread(string, fsize, 1, f);
  326.     fclose(f);
  327.  
  328.     //string[fsize] = 0;
  329.     //return string;
  330.     return fsize;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement