Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Device.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <Device.h>
9 
10 using namespace std;
11 
13 unsigned char Device::buffer[BUFFER_SIZE];
14 
18 static void initPython()
19 {
20  int parentPid = getpid();
21 
22  // Creates a whole new process
23  Device::pythonPid = fork();
24  if(Device::pythonPid == -1)
25  {
26  cout << "Problems while initiazing a new process!!!" << endl;
27  perror("Reason");
28  exit(-1);
29  }
30 
31  // pid > 0 means this is still parent process
32  if(Device::pythonPid > 0)
33  {
34  cout << "Initiating python program with pid: " << Device::pythonPid << endl;
35  return; // so we just keep doing our amazing job
36  }
37 
38  // If we got here, from the line bellow until exit(0) means totaly the child process
39  cout << "$ " << PYTHON_PROGRAM << " " << parentPid << endl;
40  char strPid[16]; // Gotta convert pid into a string
41  sprintf(strPid, "%i", parentPid);
42  int rc = execl(PYTHON_PROGRAM, "main.py", strPid, NULL);
43  if(rc == -1)
44  {
45  cout << "Error while initiating python program: " << rc << endl;
46  perror("Reason");
47  }
48  exit(0);
49 }
50 
52 {
54  initPython();
55 }
56 
58 {
59  if(Device::pythonPid > 0)
60  {
61  cout << "Killing python process: " << Device::pythonPid << endl;
62  kill(Device::pythonPid, SIGTERM);
63  }
64 }
static void destroy()
Definition: Device.cpp:57
static void init()
Definition: Device.cpp:51
#define NULL
#define PYTHON_PROGRAM
Definition: Device.h:7
static void initPython()
Definition: Device.cpp:18
static unsigned char buffer[BUFFER_SIZE]
Definition: Device.h:29
static int pythonPid
Definition: Device.h:16
#define BUFFER_SIZE
Definition: Device.h:6