RPi Locator and Display Services
para_display_client.c
Go to the documentation of this file.
1 
8 #include "skn_network_helpers.h"
9 
10 #define kXADCPATH "/sys/bus/iio/devices/iio:device0/"
11 
12 
13 int sknGetConstants(int *nOffset, float *fScale) {
14  char strRead[SZ_INFO_BUFF];
15  FILE *sysfile;
16 
17  if((sysfile = fopen(kXADCPATH "in_temp0_offset", "ra")) == NULL) {
18  fprintf(stderr, "ERROR: Can't open offset device file\n");
19  return 1;
20  }
21  fgets(strRead, SZ_INFO_BUFF-1, sysfile);
22  fclose(sysfile);
23  *nOffset = atoi(strRead);
24 
25  if((sysfile = fopen(kXADCPATH "in_temp0_scale", "ra")) == NULL) {
26  fprintf(stderr, "ERROR: Can't open scale device file\n");
27  return 2;
28  }
29  fgets(strRead, SZ_INFO_BUFF-1, sysfile);
30  fclose(sysfile);
31  *fScale = atof(strRead);
32 
33  return 0;
34 }
35 
39 int sknGetTemp(double *fTemp, double *cTemp, int nOffset, float fScale) {
40  FILE *sysfile;
41  char strRead[SZ_INFO_BUFF];
42  int nRaw;
43 
44  if(nOffset == 0 || fScale == 0.0)
45  return 1;
46 
47  if((sysfile = fopen(kXADCPATH "in_temp0_raw", "ra")) == NULL) {
48  return 2;
49  }
50 
51  fgets(strRead, SZ_INFO_BUFF-1, sysfile);
52  fclose(sysfile);
53  nRaw = atoi(strRead);
54 
55  *fTemp = (double ) (nRaw + nOffset) * fScale / 1000.0;
56 
57  *cTemp = (double ) (*fTemp - 32.0) * 5 / 9;
58 
59  return 0;
60 }
61 
62 
63 int main(int argc, char *argv[])
64 {
65  char request[SZ_INFO_BUFF];
66  char registry[SZ_CHAR_BUFF];
67  PServiceRegistry psr = NULL;
68  PRegistryEntry pre = NULL;
69  PServiceRequest pnsr = NULL;
70  int vIndex = 0;
71  int nOffset = 0;
72  float fScale = 0.0;
73 
74  memset(registry, 0, sizeof(registry));
75  memset(request, 0, sizeof(request));
76  strncpy(registry, "DisplayClient: Raspberry Pi where are you?", sizeof(registry) - 1);
77 
79  "para_display_client",
80  "Send Epiphany III ( Zynq ) Temperature to Display Service."
81  );
82 
83  /* Parse any command line options,
84  * like request string override */
85  if (skn_handle_locator_command_line(argc, argv) == EXIT_FAILURE) {
86  exit(EXIT_FAILURE);
87  }
88  if (gd_pch_message != NULL) {
89  strncpy(request, gd_pch_message, sizeof(request));
90  free(gd_pch_message); // from strdup()
91  gd_pch_message = request;
92  } else if (argc == 2) {
93  strcpy(request, argv[1]);
94  }
95 
96  skn_logger(SD_DEBUG, "Request Message [%s]", request);
97  skn_logger(SD_DEBUG, "Registry Message [%s]", registry);
98 
99  // get some platform constants
100  if(sknGetConstants(&nOffset, &fScale)) {
101  skn_logger(SD_ERR, "GetConstants() Failed! Shutting Down!");
102  exit(EXIT_FAILURE);
103  }
104 
105  /* Initialize Signal handler */
106  signals_init();
107 
108  /* Create local socket for sending requests */
110  if (gd_i_socket == EXIT_FAILURE) {
112  exit(EXIT_FAILURE);
113  }
114 
115  skn_logger(SD_NOTICE, "Application Active...");
116 
117  /* Get the ServiceRegistry from Provider
118  * - could return null if error */
120  if (psr != NULL && service_registry_entry_count(psr) != 0) {
121  char *service_name = "lcd_display_service";
122 
123  if (gd_pch_service_name != NULL) {
124  service_name = gd_pch_service_name;
125  }
126 
127  /* find a single entry */
128  pre = service_registry_find_entry(psr, service_name);
129  if (pre != NULL) {
130  skn_logger(" ", "\nLCD DisplayService (%s) is located at IPv4: %s:%d\n", pre->name, pre->ip, pre->port);
131  }
132 
133  /*
134  * Switch to non-broadcast type socket */
135  close(gd_i_socket); gd_i_socket = -1;
137  if (gd_i_socket == EXIT_FAILURE) {
138  if (psr != NULL) service_registry_destroy(psr);
140  exit(EXIT_FAILURE);
141  }
142  }
143 
144  // we have the location
145  if (pre != NULL) {
146  if (request[0] == 0) {
147  snprintf(request, sizeof(request), "%02ld Cores Available.", skn_get_number_of_cpu_cores() );
148  }
149  pnsr = skn_service_request_create(pre, gd_i_socket, request);
150  }
151  if (pnsr != NULL) {
152  double fTemp = 0.0;
153  double cTemp = 0.0;
154  do {
155  vIndex = skn_udp_service_request(pnsr);
156  if ((vIndex == EXIT_FAILURE) && (gd_i_update == 0)) { // ignore if non-stop is set
157  break;
158  }
159  sleep(gd_i_update);
160 
161  sknGetTemp(&fTemp, &cTemp, nOffset, fScale);
162  snprintf(pnsr->request, sizeof(pnsr->request) -1, "%.1fF %.1fC", fTemp, cTemp);
163 
164 
165  } while(gd_i_update != 0 && gi_exit_flag == SKN_RUN_MODE_RUN);
166  free(pnsr); // Done
167 
168  } else {
169  skn_logger(SD_WARNING, "Unable to create Network Request.");
170  }
171 
172  /* Cleanup and shutdown
173  * - if shutdown was caused by signal handler
174  * then a termination signal will be sent via signal()
175  * otherwise, a normal exit occurs
176  */
177  if (gd_i_socket) close(gd_i_socket);
178  if (psr != NULL) service_registry_destroy(psr);
180 
181  exit(EXIT_SUCCESS);
182 }
long skn_get_number_of_cpu_cores()
int skn_udp_host_create_regular_socket(int port, double rcvTimeout)
char * gd_pch_service_name
int sknGetConstants(int *nOffset, float *fScale)
#define kXADCPATH
int service_registry_entry_count(PServiceRegistry psr)
#define SD_DEBUG
int port
void signals_init()
#define SZ_CHAR_BUFF
Definition: cmdDC.c:57
int gd_i_update
void signals_cleanup(int sig)
int skn_handle_locator_command_line(int argc, char **argv)
char * gd_pch_message
#define SD_ERR
#define SD_WARNING
#define SZ_INFO_BUFF
Definition: cmdDC.c:56
PServiceRegistry service_registry_get_via_udp_broadcast(int i_socket, char *request)
int sknGetTemp(double *fTemp, double *cTemp, int nOffset, float fScale)
#define SKN_RUN_MODE_RUN
char name[SZ_INFO_BUFF]
sig_atomic_t gi_exit_flag
void service_registry_destroy(PServiceRegistry psreg)
char request[SZ_INFO_BUFF]
PServiceRequest skn_service_request_create(PRegistryEntry pre, int host_socket, char *request)
#define SD_NOTICE
int skn_udp_service_request(PServiceRequest psr)
int skn_udp_host_create_broadcast_socket(int port, double rcvTimeout)
char ip[SZ_INFO_BUFF]
void skn_program_name_and_description_set(const char *name, const char *desc)
int main(int argc, char *argv[])
PRegistryEntry service_registry_find_entry(PServiceRegistry psreg, char *serviceName)
int gd_i_socket