RPi Locator and Display Services
lcd_display_client.c File Reference
+ Include dependency graph for lcd_display_client.c:

Go to the source code of this file.

Functions

long getCpuTemps (PCpuTemps temps)
 
int generate_cpu_temps_info (char *msg)
 
int main (int argc, char *argv[])
 

Function Documentation

int generate_cpu_temps_info ( char *  msg)

DO NOT USE THIS IN MODULES THAT HANDLE A I2C Based LCD RPi cannot handle I2C and GetCpuTemp() without locking the process in an uniterrupted sleep; forcing a power cycle.

Definition at line 49 of file lcd_display_client.c.

Referenced by main().

49  {
50  static CpuTemps cpuTemp;
51  int mLen = 0;
52 
53  memset(&cpuTemp, 0, sizeof(CpuTemps));
54  if ( getCpuTemps(&cpuTemp) != -1 ) {
55  mLen = snprintf(msg, SZ_INFO_BUFF-1, "%s %s", cpuTemp.c, cpuTemp.f);
56  } else {
57  mLen = snprintf(msg, SZ_INFO_BUFF-1, "Temp: N/A");
58  }
59 
60  return mLen;
61 }
long getCpuTemps(PCpuTemps temps)
char c[SZ_CHAR_LABEL]
#define SZ_INFO_BUFF
Definition: cmdDC.c:56
char f[SZ_CHAR_LABEL]

+ Here is the caller graph for this function:

long getCpuTemps ( PCpuTemps  temps)

udp_locator_client.c

  • Client

cmdline: ./udp_locator_client -m "<request-message-string>" DO NOT USE THIS IN MODULES THAT HANDLE A I2C Based LCD RPi cannot handle I2C and GetCpuTemp() without locking the process in an uniterrupted sleep; forcing a power cycle.

Definition at line 16 of file lcd_display_client.c.

References _temps::c, _temps::cbName, _temps::f, _temps::raw, and SD_WARNING.

Referenced by generate_cpu_temps_info().

16  {
17  long lRaw = 0;
18  int rc = 0;
19 
20  FILE *sysFs = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
21  if (sysFs == NULL) {
22  skn_logger(SD_WARNING, "Warning: Failed to OPEN CPU temperature: %d:%s\n", errno, strerror(errno));
23  return -1;
24  }
25 
26  rc = fscanf(sysFs, "%ld", &lRaw);
27  fclose(sysFs);
28 
29  if (rc != 1 || lRaw < 0) {
30  skn_logger(SD_WARNING, "Warning: Failed to READ CPU temperature: %d:%s\n", strerror(errno));
31  return -1;
32  }
33 
34  if (temps != NULL) { // populate struct
35  snprintf(temps->c, sizeof(temps->c), "%3.1f%cC", (double )(lRaw / 1000.0), 223);
36  snprintf(temps->f, sizeof(temps->f), "%3.1f%cF", (double )(lRaw / 1000.0 * 9 / 5 + 32), 223);
37  temps->raw = lRaw;
38  strncpy(temps->cbName, "CpuTemps", sizeof(temps->cbName) - 1);
39  }
40 
41  return lRaw;
42 }
char c[SZ_CHAR_LABEL]
#define SD_WARNING
char f[SZ_CHAR_LABEL]
char cbName[SZ_CHAR_LABEL]

+ Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 64 of file lcd_display_client.c.

References gd_i_socket, gd_i_update, gd_pch_message, gd_pch_service_name, generate_cpu_temps_info(), generate_datetime_info(), generate_loadavg_info(), generate_uname_info(), gi_exit_flag, _serviceEntry::ip, _serviceEntry::name, _serviceEntry::port, _serviceRequest::request, SD_DEBUG, SD_NOTICE, SD_WARNING, service_registry_destroy(), service_registry_entry_count(), service_registry_find_entry(), service_registry_get_via_udp_broadcast(), signals_cleanup(), signals_init(), skn_get_number_of_cpu_cores(), skn_handle_locator_command_line(), skn_program_name_and_description_set(), SKN_RUN_MODE_RUN, skn_service_request_create(), skn_udp_host_create_broadcast_socket(), skn_udp_host_create_regular_socket(), skn_udp_service_request(), SZ_CHAR_BUFF, and SZ_INFO_BUFF.

65 {
66  char request[SZ_INFO_BUFF];
67  char registry[SZ_CHAR_BUFF];
68  PServiceRegistry psr = NULL;
69  PRegistryEntry pre = NULL;
70  PServiceRequest pnsr = NULL;
71  int vIndex = 0;
72  long host_update_cycle = 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  "lcd_display_client",
80  "Send messages to the 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  /* Initialize Signal handler */
100  signals_init();
101 
102  /* Create local socket for sending requests */
104  if (gd_i_socket == EXIT_FAILURE) {
106  exit(EXIT_FAILURE);
107  }
108 
109  skn_logger(SD_NOTICE, "Application Active...");
110 
111  /* Get the ServiceRegistry from Provider
112  * - could return null if error */
114  if (psr != NULL && service_registry_entry_count(psr) != 0) {
115  char *service_name = "lcd_display_service";
116 
117  if (gd_pch_service_name != NULL) {
118  service_name = gd_pch_service_name;
119  }
120 
121  /* find a single entry */
122  pre = service_registry_find_entry(psr, service_name);
123  if (pre != NULL) {
124  skn_logger(" ", "\nLCD DisplayService (%s) is located at IPv4: %s:%d\n", pre->name, pre->ip, pre->port);
125  }
126 
127  /*
128  * Switch to non-broadcast type socket */
129  close(gd_i_socket); gd_i_socket = -1;
131  if (gd_i_socket == EXIT_FAILURE) {
132  if (psr != NULL) service_registry_destroy(psr);
134  exit(EXIT_FAILURE);
135  }
136  }
137 
138 
139  // we have the location
140  if (pre != NULL) {
141  if (request[0] == 0) {
142  snprintf(request, sizeof(request), "%02ld Cores Available.", skn_get_number_of_cpu_cores() );
143  }
144  pnsr = skn_service_request_create(pre, gd_i_socket, request);
145  }
146  if (pnsr != NULL) {
147  do {
148  vIndex = skn_udp_service_request(pnsr);
149  if ((vIndex == EXIT_FAILURE) && (gd_i_update == 0)) { // ignore if non-stop is set
150  break;
151  }
152  sleep(gd_i_update);
153 
154  switch (host_update_cycle++) { // cycle through other info
155  case 0:
157  break;
158  case 1:
160  break;
161  case 2:
163  break;
164  case 3:
166  host_update_cycle = 0;
167  break;
168  }
169 
170  } while(gd_i_update != 0 && gi_exit_flag == SKN_RUN_MODE_RUN);
171  free(pnsr); // Done
172 
173  } else {
174  skn_logger(SD_WARNING, "Unable to create Network Request.");
175  }
176 
177  /* Cleanup and shutdown
178  * - if shutdown was caused by signal handler
179  * then a termination signal will be sent via signal()
180  * otherwise, a normal exit occurs
181  */
182  if (gd_i_socket) close(gd_i_socket);
183  if (psr != NULL) service_registry_destroy(psr);
185 
186  exit(EXIT_SUCCESS);
187 }
long skn_get_number_of_cpu_cores()
int skn_udp_host_create_regular_socket(int port, double rcvTimeout)
char * gd_pch_service_name
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_WARNING
#define SZ_INFO_BUFF
Definition: cmdDC.c:56
PServiceRegistry service_registry_get_via_udp_broadcast(int i_socket, char *request)
int generate_uname_info(char *msg)
#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)
int generate_datetime_info(char *msg)
int generate_loadavg_info(char *msg)
char ip[SZ_INFO_BUFF]
void skn_program_name_and_description_set(const char *name, const char *desc)
int generate_cpu_temps_info(char *msg)
PRegistryEntry service_registry_find_entry(PServiceRegistry psreg, char *serviceName)
int gd_i_socket

+ Here is the call graph for this function: