├── LICENSE ├── README.md └── wizfs.c /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 adventuresin9 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wizfs 2 | A 9Front file server for interacting with Philips Wiz light bulbs 3 | by adventuresin9 4 | 5 | This uses json.h which is available in 9Front. 6 | 7 | usage: wizfs -m "mount point" -s "srv name" -d (set debug true) 8 | 9 | The defaults are to mount "wiz" in /n, and post "wizfs" to /srv. 10 | 11 | The commands that can be sent to the bulbs; 12 | 13 | on : sends "state":true to turn the bulb on 14 | off : send "state":false to turn the bulb off 15 | pulse: uses the pulse method to make the bulb dim for half a second 16 | 17 | All other commands have to be sent as a key=value pair. 18 | Known working options are: 19 | 20 | state=(0 or 1)(false or true) turns the bulb off or on 21 | r=(0-255) for the red LED 22 | g=(0-255) for green 23 | b=(0-255) for blue 24 | c=(0-255) for cool white 25 | w=(0-255) for warm white 26 | temp=(2200-6500) for warm to cool white light 27 | dimming=(10-100) dims the bulbs 28 | sceneId=(0-32) sets one of the prepackaged "scenes", note the capital I in Id. 29 | speed=(0-200) used for sceneId that cycles through colors, makes is go faster or slower 30 | 31 | Sometimes the bulb will send back an error if the value is outside a given range. 32 | Other times it will report success, but the value might be looped back around to 0. 33 | Like, b=265 looks like b= 10. So bits over a certain value may be truncated by the bulb. 34 | 35 | NEW; 36 | 37 | I have found that this works as is with the Wiz brand Smart Plug. It will turn the plug on and off using the same commands that turn the bulbs on and off. 38 | -------------------------------------------------------------------------------- /wizfs.c: -------------------------------------------------------------------------------- 1 | /*************************************************************/ 2 | /* wizfs.c v1.0 */ 3 | /* by adventuresin9 */ 4 | /* */ 5 | /* With the default options, this will post "wizfs" to /srv */ 6 | /* and mount the file system in /n with a directory called */ 7 | /* "wiz". */ 8 | /* */ 9 | /* /lib/ndb/local will be checked, and any line with */ 10 | /* "wiz=bulb" will use the "sys=" entry to make a bulb file. */ 11 | /* */ 12 | /* Reading the bulb files will output the results from the */ 13 | /* getPilot function of the Philips Wiz bulbs. */ 14 | /* */ 15 | /* Writing will take "on", "off", and "pulse", as single */ 16 | /* word commands, and report an error for others. All other */ 17 | /* commands must be in a "key=value" format. Errors from */ 18 | /* the bulb about bad commands will be reported back. */ 19 | /* */ 20 | /*************************************************************/ 21 | 22 | 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include <9p.h> 30 | #include 31 | #include 32 | 33 | #define PILOT "{\"method\":\"getPilot\",\"params\":{}}" 34 | #define SYSTEM "{\"method\":\"getSystemConfig\", \"params\":{}}" 35 | #define PULSE "{\"method\":\"pulse\",\"params\":{\"delta\":-50,\"duration\":500}}" 36 | #define WIZOFF "{\"method\": \"setPilot\", \"id\": 24, \"params\": {\"state\": false}}" 37 | #define WIZON "{\"method\": \"setPilot\", \"id\": 24, \"params\": {\"state\": true}}" 38 | #define NOBULB "Bulb unreachable ☹" 39 | 40 | typedef struct Bulbcmd Bulbcmd; 41 | 42 | struct Bulbcmd 43 | { 44 | char *key; 45 | char *value; 46 | }; 47 | 48 | 49 | void fsread(Req *r); 50 | void fswrite(Req *r); 51 | void fsstart(Srv*); 52 | void fsend(Srv*); 53 | 54 | 55 | Srv fs = 56 | { 57 | .start=fsstart, 58 | .read=fsread, 59 | .write=fswrite, 60 | .end=fsend, 61 | }; 62 | 63 | 64 | File *root; 65 | File *wizdir; 66 | char *mtpt; 67 | char *service; 68 | int debug = 0; 69 | 70 | 71 | void 72 | timeout(void *, char *msg) 73 | { 74 | if(strstr("alarm", msg) != nil) 75 | noted(NCONT); 76 | else 77 | noted(NDFLT); 78 | } 79 | 80 | 81 | int 82 | callbulb(char *bulbname, char *bulbcmd, char *bulbreply, long brsize) 83 | { 84 | int fd, n; 85 | n = 0; 86 | 87 | if(debug) 88 | print("bulbname %s\nbulbcmd %s\nmkaddr %s\n", bulbname, bulbcmd, netmkaddr(bulbname, "upd", "38899")); 89 | 90 | notify(timeout); 91 | alarm(1000); 92 | 93 | fd = dial(netmkaddr(bulbname, "udp", "38899"), nil, nil, nil); 94 | fprint(fd, bulbcmd); 95 | sleep(10); 96 | n = read(fd, bulbreply, brsize); 97 | 98 | alarm(0); 99 | 100 | if(debug) 101 | print("fd is %d\nbulbreply %s\n", fd, bulbreply); 102 | 103 | close(fd); 104 | return(n); 105 | } 106 | 107 | 108 | int 109 | jtoresult(JSON *jreply, char *out, long nout) 110 | { 111 | JSON *jresult; 112 | JSONEl *jp; 113 | char *p; 114 | int n = 0; 115 | 116 | jresult = jsonbyname(jreply, "result"); 117 | 118 | jp = jresult->first; 119 | p = out; 120 | 121 | while(jp != nil){ 122 | p = seprint(p, out + nout, jp->name); 123 | 124 | switch(jp->val->t){ 125 | case JSONNull: 126 | p = seprint(p, out + nout, "=‽\n"); 127 | break; 128 | case JSONBool: 129 | p = seprint(p, out + nout, "=%s\n", jp->val->n ? "true" : "false"); 130 | break; 131 | case JSONNumber: 132 | p = seprint(p, out + nout, "=%.f\n", jp->val->n); 133 | break; 134 | case JSONString: 135 | p = seprint(p, out + nout, "=%s\n", jp->val->s); 136 | break; 137 | } 138 | n++; 139 | jp = jp->next; 140 | } 141 | 142 | USED(p); 143 | USED(jp); 144 | return(n); 145 | } 146 | 147 | 148 | int 149 | jtoerror(JSON *jreply, char *error) 150 | { 151 | JSON *jerror; 152 | JSON *jmsg; 153 | int n = 0; 154 | 155 | jerror = jsonbyname(jreply, "error"); 156 | 157 | if(jerror != nil) 158 | jmsg = jsonbyname(jerror, "message"); 159 | 160 | if(jmsg != nil){ 161 | strcpy(error, jmsg->s); 162 | n = strlen(error); 163 | } 164 | 165 | return(n); 166 | } 167 | 168 | 169 | int 170 | makebulbcmd(char *input, char *out, long nout) 171 | { 172 | Bulbcmd bc[16]; 173 | 174 | char *pair[16]; 175 | char *command[2]; 176 | char *p; 177 | 178 | int i, ti, ci; 179 | 180 | if(debug) 181 | print("input %s\n", input); 182 | 183 | ti = tokenize(input, pair, 16); 184 | 185 | if(!ti) 186 | return(ti); 187 | 188 | for(i = 0; i < ti; i++){ 189 | ci = getfields(pair[i], command, 2, 1, "="); 190 | if(ci == 2){ 191 | bc[i].key = command[0]; 192 | bc[i].value = command[1]; 193 | } 194 | else{ 195 | bc[i].key = command[0]; 196 | if(!strcmp(bc[0].key, "pulse")){ 197 | strcpy(out, PULSE); 198 | return(1); 199 | }else if(!strcmp(bc[0].key, "on")){ 200 | strcpy(out, WIZON); 201 | return(1); 202 | }else if(!strcmp(bc[0].key, "off")){ 203 | strcpy(out, WIZOFF); 204 | return(1); 205 | } 206 | return(0); 207 | } 208 | } 209 | 210 | p = out; 211 | p = seprint(p, out + nout, "{\"method\":\"setPilot\",\"id\":%d,\"params\":{", time(0)); 212 | 213 | i = 0; 214 | while(i < (ti - 1)){ 215 | p = seprint(p, out + nout, "\"%s\":%s,", bc[i].key, bc[i].value); 216 | i++; 217 | } 218 | 219 | p = seprint(p, out + nout, "\"%s\":%s}}", bc[i].key, bc[i].value); 220 | 221 | USED(p); 222 | 223 | return(strlen(out)); 224 | } 225 | 226 | 227 | void 228 | fsmkdir(void) 229 | { 230 | Ndb *bulbndb; 231 | Ndbs s; 232 | Ndbtuple *bulbtp; 233 | char *sysname; 234 | char *user; 235 | 236 | /* this assumes the system name "sys=" is the first entry for a line in ndb/local */ 237 | /* all bulbs on the network must have "wiz=bulb" to be included in the file system */ 238 | 239 | user = getuser(); 240 | fs.tree = alloctree(user, user, 0555, nil); 241 | 242 | root = fs.tree->root; 243 | 244 | wizdir = createfile(root, "wiz", user, DMDIR|0555, nil); 245 | 246 | bulbndb = ndbopen(0); 247 | 248 | for(bulbtp = ndbsearch(bulbndb, &s, "wiz", "bulb"); bulbtp != nil; bulbtp = ndbsnext(&s, "wiz", "bulb")){ 249 | sysname = bulbtp->val; 250 | createfile(wizdir, sysname, user, 0666, nil); 251 | } 252 | 253 | ndbclose(bulbndb); 254 | } 255 | 256 | 257 | void 258 | fsread(Req *r) 259 | { 260 | char bulbreply[1024]; 261 | JSON *jreply; 262 | char waserror[64]; 263 | char *rerror; 264 | char *bulbname; 265 | char readout[1024]; 266 | 267 | memset(bulbreply, 0, 1024); 268 | memset(readout, 0, 1024); 269 | rerror = nil; 270 | 271 | bulbname = r->fid->file->name; 272 | 273 | if(callbulb(bulbname, PILOT, bulbreply, sizeof(bulbreply)) < 1){ 274 | respond(r, NOBULB); 275 | return; 276 | } 277 | 278 | jreply = jsonparse(bulbreply); 279 | 280 | if(jtoresult(jreply, readout, sizeof(readout)) < 1){ 281 | jtoerror(jreply, waserror); 282 | rerror = waserror; 283 | } 284 | 285 | readstr(r, readout); 286 | jsonfree(jreply); 287 | 288 | respond(r, rerror); 289 | } 290 | 291 | 292 | 293 | void 294 | fswrite(Req *r) 295 | { 296 | int n; 297 | char *input; 298 | char *rerror; 299 | char waserror[64]; 300 | char bulbcmd[1024]; 301 | char bulbreply[1024]; 302 | JSON *jreply; 303 | 304 | memset(bulbcmd, 0, 1024); 305 | memset(bulbreply, 0, 1024); 306 | rerror = nil; 307 | 308 | n = r->ofcall.count = r->ifcall.count; 309 | input = emalloc9p(n+1); 310 | memmove(input, r->ifcall.data, n); 311 | 312 | if(makebulbcmd(input, bulbcmd, sizeof(bulbcmd)) < 1){ 313 | respond(r, "makebulbcmd failed"); 314 | free(input); 315 | return; 316 | } 317 | 318 | if(callbulb(r->fid->file->name, bulbcmd, bulbreply, sizeof(bulbreply)) < 1){ 319 | respond(r, NOBULB); 320 | free(input); 321 | return; 322 | } 323 | 324 | jreply = jsonparse(bulbreply); 325 | 326 | if(jtoerror(jreply, waserror)) 327 | rerror = waserror; 328 | 329 | respond(r, rerror); 330 | free(input); 331 | jsonfree(jreply); 332 | } 333 | 334 | 335 | void 336 | fsstart(Srv*) 337 | { 338 | fsmkdir(); 339 | } 340 | 341 | 342 | void 343 | fsend(Srv*) 344 | { 345 | postnote(PNGROUP, getpid(), "shutdown"); 346 | threadexitsall(nil); 347 | } 348 | 349 | 350 | void 351 | usage(void) 352 | { 353 | fprint(2, "usage: %s [-d] [-m mtpt] [-s service]\n", argv0); 354 | exits("usage"); 355 | } 356 | 357 | 358 | void 359 | threadmain(int argc, char *argv[]) 360 | { 361 | mtpt = "/n"; 362 | service = "wizfs"; 363 | 364 | ARGBEGIN { 365 | case 'm': 366 | mtpt = EARGF(usage()); 367 | break; 368 | case 's': 369 | service = EARGF(usage()); 370 | break; 371 | case 'd': 372 | debug++; 373 | break; 374 | default: 375 | usage(); 376 | } ARGEND; 377 | 378 | threadpostmountsrv(&fs, service, mtpt, MREPL|MCREATE); 379 | threadexits(nil); 380 | } 381 | --------------------------------------------------------------------------------