ba399f0c0035944db380ae164bc1f3c1d6cb0592
[monky] / src / bme.c
1 /* 
2  * File:   bme.C
3  * Author: Lance
4  * 
5  * Created on March 6, 2012, 11:09 PM
6  */
7
8 //#include "bme.h"
9
10 /*
11 ** Copyright 2012, Alexey Roslyakov <alexey.roslyakov@newsycat.com>
12 **
13 ** Licensed under the Apache License, Version 2.0 (the "License");
14 ** you may not use this file except in compliance with the License.
15 ** You may obtain a copy of the License at
16 **
17 **     http://www.apache.org/licenses/LICENSE-2.0
18 **
19 ** Unless required by applicable law or agreed to in writing, software
20 ** distributed under the License is distributed on an "AS IS" BASIS,
21 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 ** See the License for the specific language governing permissions and
23 ** limitations under the License.
24 */
25
26 // from here: https://gitorious.org/harmattan-goodies/bmecli/blobs/master/bmecli.c
27
28 #include <unistd.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34
35 typedef unsigned int uint32;
36 typedef unsigned short uint16;
37 typedef unsigned char uint8;
38
39 //const char DefaultSocketPath[] = "/tmp/.bmesrv";
40
41 struct bme_reply { //22 items
42   uint32     unknown1;
43   uint32     unknown2;
44   uint32     charger_type; // 0 - none, 2 - usbmax500, 3 - usbwall
45   uint32     unknown4;
46   uint32     unknown5;
47   uint32     charging_time;
48   uint32     unknown7;
49   uint32     unknown8;
50   uint32     battery_max_level; // - cur level?
51   uint32     battery_cur_level; // /
52   uint32     unknown11;
53   uint32     unknown12;
54   uint32     battery_max_capacity;
55   uint32     battery_cur_capacity;
56   uint32     battery_max_voltage;
57   uint32     battery_cur_voltage;
58   uint32     battery_pct_level;
59   uint32     battery_temperature;
60   int        battery_current;
61   uint32     unknown20;
62   uint32     battery_last_full_capacity;
63   uint8      reserved[128];
64 };
65
66 static void dumpBuffer(const void *buf, size_t bufSize)
67 {
68   int cnt = 0; char *p = (char*)buf;
69   while (bufSize--)
70     fprintf(stderr, "%02X%s", *p++, (++cnt & 15 ? " " : "\n") );
71   fprintf(stderr, "\n");
72 }
73
74 const char* bmeClientGetData(const char *bmeSocketPath, struct bme_reply *bmeReply)
75 {
76   static int x = 0;
77   printf("%d\n", x); // outputs the value of x //always crashes on 23 or 24
78   x = x + 1;
79     
80   int s = socket(AF_LOCAL, SOCK_STREAM, 0);
81   if (s <= 0)
82     return "socket";
83   NORM_ERR("bme a");
84   struct sockaddr_un name;
85   name.sun_family = AF_LOCAL;
86   strcpy(name.sun_path, bmeSocketPath);
87   if ( connect(s, (struct sockaddr *)&name, sizeof(name)) != 0)
88     return "connect";
89
90   char foo[256];
91   size_t rd;
92   NORM_ERR("bme b");
93   // TODO: clean up this crap
94   write(s, "SYNC\10\0\0\0BMentity", 16);
95   rd = read(s, foo, sizeof(foo));
96   NORM_ERR("read %i bytes",rd); //reads 0 right before it crashes
97   //fprintf(stderr, "rd=%zu\n", rd);
98   NORM_ERR("bme c");
99   write(s, "SYNC\4\0\0\0\3\200\0\0\n", 12);//crashes here
100   NORM_ERR("bme c.5");
101   rd = read(s, foo, 20);
102   NORM_ERR("bme d");
103   NORM_ERR("read %i bytes",rd); //reads 0 right before it crashes
104   //fprintf(stderr, "rd=%zu\n", rd);
105   rd = read(s, bmeReply, sizeof(*bmeReply));
106   //fprintf(stderr, "rd=%zu\n", rd);
107   NORM_ERR("bme e");
108   // FIXME: sometimes we get extra data in replies
109   NORM_ERR("read %i bytes",rd); //reads 0 right before it crashes
110   if (rd > 128){
111     memmove( bmeReply, ((char*) bmeReply) + rd - 128, 128);
112   }
113   dumpBuffer(bmeReply, rd);
114
115   return 0;
116 }
117
118 //int print_usage(const char *prog)
119 //{
120 //  fprintf(stderr, "Usage: %s <bme_socket_path>\n", prog);
121 //  fprintf(stderr, "sizeof=%zu\n", sizeof (struct bme_reply));
122 //
123 //  return -1;
124 //}
125
126 struct bme_reply getBattInfoFromBME()
127 {
128 //  if (argc != 2)
129 //    return print_usage(argv[0]);
130
131   struct bme_reply bmeReply;
132   
133   memset(&bmeReply, 0, sizeof bmeReply);
134   NORM_ERR("bme 1");
135   const char *failedOn = bmeClientGetData("/tmp/.bmesrv", &bmeReply);
136   NORM_ERR("bme 2");
137   if (failedOn) {
138     NORM_ERR("bme_client_get_data failed on %s: %s\n", failedOn, strerror(errno));
139     //try again
140     failedOn = 0;
141     const char *failedOn = bmeClientGetData("/tmp/.bmesrv", &bmeReply);
142     if (failedOn){//still failed
143         NORM_ERR("failed 2x in a row @ bme");        
144         bmeReply.battery_temperature = -1;
145         bmeReply.battery_current = 0;
146         return bmeReply;
147     }
148   }
149
150
151 /*
152   fprintf(stderr, "BME stat\n");
153   fprintf(stderr, "Unknown 1,2,4,5: %u %u %u %u\n", bmeReply.unknown1, bmeReply.unknown2, bmeReply.unknown4, bmeReply.unknown5);
154   fprintf(stderr, "Charger type:\t\t%u\n", bmeReply.charger_type);
155   fprintf(stderr, "Charging time:\t\t%u\n", bmeReply.charging_time);
156   fprintf(stderr, "Battery temperature:\t%.2f\n", bmeReply.battery_temperature - 273.15f);
157   fprintf(stderr, "Battery max. Level:\t%u\n", bmeReply.battery_max_level);
158   fprintf(stderr, "Battery cur. Level:\t%u\n", bmeReply.battery_cur_level);
159   fprintf(stderr, "Battery pct. Level:\t%u\n", bmeReply.battery_pct_level);
160   fprintf(stderr, "Battery Max. Capacity:\t%u\n", bmeReply.battery_max_capacity);
161   fprintf(stderr, "Battery Cur. Capacity:\t%u\n", bmeReply.battery_cur_capacity);
162   fprintf(stderr, "Battery Last Full Cap:\t%u\n", bmeReply.battery_last_full_capacity);
163   fprintf(stderr, "Battery Max. Voltage:\t%u\n", bmeReply.battery_max_voltage);
164   fprintf(stderr, "Battery Cur. Voltage:\t%u\n", bmeReply.battery_cur_voltage);
165   fprintf(stderr, "Battery Current:\t%d\n", bmeReply.battery_current);
166   fprintf(stderr, "Unknown 7,8:\t%u %u\n", bmeReply.unknown7, bmeReply.unknown7);
167   fprintf(stderr, "Unknown 11,12:\t%u %u\n", bmeReply.unknown11, bmeReply.unknown12);
168   fprintf(stderr, "Unknown20 (timestamp?):\t%u\n", bmeReply.unknown20);
169   fprintf(stderr, "\n");  
170 */
171
172
173   return bmeReply;
174 }
175
176