“AB Shutter 3″という激安Bluetoothシャッターリモコンは多くの方が情報公開されているのでそちらを参照していただくとして...
Raspberry Piで使うためにはBlueButtonを使うのが手っ取り早くて簡単なのだが2個あるボタンを判別できないという点が気になったので独自に作成してみた。
実行中のローカル・コンソール・アプリの標準入力へ各ボタンのイベントに設定した文字列、或いは、-cオプションによりコマンドラインから任意の文字列を出力することが可能。実行にはルート権限が必要なことに注意。
AB Shutter 3のイベント処理を行う。
sudo ./abshutter
“quit”を標準入力に出力して終了する。(リモート操作用)
sudo ./abshutter -c quit
【設定ファイル】
※戦闘の<>で囲まれた文字列のみ標準入力に出力し、それ以外はコマンドラインとして実行する。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[program] cmdline= 実行するアプリのコマンドライン(指定がない場合はスリープ) [control] quit=<q> -c オプションで指定された文字列を変換する設定(-c quit --> "q") [button-a] click=<a> 大きいボタン(IOS)をクリックしたとき long-pressed= 大きいボタン(IOS)を長押ししたとき [button-b] click=<b> 小さいボタン(andorid)をクリックしたとき long-pressed= 小さいボタン(andorid)を長押ししたとき |
単独実行しておくだけで良いのだがsystemctlなどには登録できない。起動ユーザーのログイン・スクリプトの最後にでも登録しておけばOK。ちなみにbluetoothの接続&切断にも対応している。
最後の【AB Shutter 3 ペアリング・スクリプト】を使うと自動でペアリングができる。(と思う)
【修正履歴】
2023-04-19
前回改良したところがバグッてたので修正。それとAB Shutter 3の仕様が製品により微妙に異なるようで下記製品にも対応できように改良。ダイソーで売ってたのよりこの製品の仕様のほうがすっきりしていていい感じだ。
日本語パッケージ・説明書付・技適マーク有、正規品】AB SHUTTER 3 スマートフォンカメラ用リモコン
2023-04-15
バグ修正ついでにイベント発生時に標準出力への文字列出力とコマンドライン実行の両方を設定できるように改良。
1 |
例: click=<a>cmdline |
2023-04-13
イベント発生時にコマンドライン実行できるように改良。標準出力に文字列を出力する場合は<>で囲んだ文字列を指定する。
ダイソーから購入したのはボタン認識が出来るがアマゾンで安く売られているのはどちらのボタンも同じコードでボタン認識ができない。外見はそっくりだがダイソーのは技適マーク有りでアマゾンのは技適マーク無しと中身の異なるコピー商品のようだ。
Bluetooth スマートフォン用 カメラリモコン AB Shutter 3 【Black】 ABS3-BLK-A
【abshutter ツール】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
TARGET = abshutter OBJS = $(TARGET).o DEPFILES = $(OBJS:%.o=%.d) CC = gcc CPPFLAGS = -MMD CFLAGS = -I. -Wall LDLIBS = -L. -lstdc++ -lpthread $(TARGET): $(OBJS) $(CC) -o $@ $^ $(LDLIBS) clean: rm -f $(TARGET) $(OBJS) $(DEPFILES) -include $(DEPFILES) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
/* abshutter.cpp - AB Shutter Tool Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <signal.h> #include "abshutter.h" #include "fileprops.h" #define LONGPRESS_TIME 3000000 // us FileProps props; ABShutter3 button; UInput uinput; char config[PATH_MAX]; int exitcode; FILE *fpout; void command(const char *str) { if (*str == '<') { size_t len = strlen(str); for (size_t i = 1; i < len; ) { char c = str[i++]; if (c == '\\') i++; else if (c == '>') { if (i > 2) { if (fpout) { fwrite(str + 1, 1, i - 2, fpout); fflush(fpout); } else uinput.keystr(str + 1, i - 2); } str += i; break; } } } if (*str) system(str); } long subtime(struct timeval &a, struct timeval &b) { return ((long long)a.tv_sec * 1000000 + a.tv_usec) - ((long long)b.tv_sec * 1000000 + b.tv_usec); } void input(struct input_event *event, void *arg) { static struct timeval pushed; const char *btn, *cmd; // printf("code=%d, value=%d\n", event->code, event->value); btn = cmd = nullptr; switch (event->code) { case ABShutter3::BUTTON_A: btn = "button-a"; break; case ABShutter3::BUTTON_B: btn = "button-b"; break; } if (event->value) { cmd = "click"; pushed = event->time; } else if (subtime(event->time, pushed) >= LONGPRESS_TIME) { cmd = "long-pressed"; } if (btn && cmd) command(props.getProperty(btn, cmd)); } void sigint_handler(int signum) { exitcode = 1; } void usage(void) { printf("AB Shutter 3 Remote Application Controller, Version 1.00\n"); printf("\n"); printf("Usage: abshutter [options]\n"); printf("\n"); printf(" options:\n"); printf(" -p ... use popen()\n"); printf(" -c ... command string\n"); printf(" -h ... this help\n"); printf("\n"); } int main(int argc, char **argv) { bool optpipe = false; // // set current directory to running process directory // snprintf(config, sizeof(config), "%s", argv[0]); char *p = strrchr(config, '/'); if (p) *p = 0; chdir(config); // // load properties // snprintf(config, sizeof(config), "%s.conf", argv[0]); props.load(config); // // command line option // int opt; while ((opt = getopt(argc, argv, "hpc:")) != -1) { switch (opt) { case 'p': optpipe = true; break; case 'c': uinput.start(); command(props.getProperty("control", optarg)); uinput.stop(); exit(0); case 'h': usage(); /* fall through */ default: exit(1); } } // // setup signal handler // struct sigaction nact, oact; sigemptyset(&nact.sa_mask); nact.sa_handler = sigint_handler; nact.sa_flags = SA_RESTART; sigaction(SIGINT , &nact, &oact); sigaction(SIGTERM, &nact, &oact); // // start AB Shutter // button.callback(input); button.start(); uinput.start(); // // run program // while (exitcode == 0) { // // reload properties // props.load(config); const char *cmd = props.getProperty("program", "cmdline"); if (*cmd) { if (!optpipe) system(cmd); else if ((fpout = popen(cmd, "w"))) pclose(fpout); } sleep(3); } // // stop AB Shutter // uinput.stop(); button.stop(); return exitcode; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
/* abshutter.h - AB Shutter Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <dirent.h> #include <pthread.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <linux/input.h> #include <linux/uinput.h> #include <string> typedef void (*input_cb_t)(struct input_event *, void *arg); class UInput { public: UInput(void) : _fd(-1) { } virtual ~UInput(void) { stop(); } void start(void) { if ((_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK)) != -1) { struct uinput_setup usetup; memset(&usetup, 0, sizeof(usetup)); usetup.id.bustype = BUS_USB; usetup.id.vendor = 0x1234; /* sample vendor */ usetup.id.product = 0x5678; /* sample product */ strcpy(usetup.name, "Example device"); ioctl(_fd, UI_SET_EVBIT, EV_KEY); for (int i = 0; i < KEY_MAX; ++i) ioctl(_fd, UI_SET_KEYBIT, i); ioctl(_fd, UI_DEV_SETUP, &usetup); ioctl(_fd, UI_DEV_CREATE); } } void stop(void) { if (_fd != -1) { ioctl(_fd, UI_DEV_DESTROY); close(_fd); _fd = -1; } } void keystr(const char *str, bool escape = true) { keystr(str, strlen(str), escape); } void keystr(const char *str, size_t len, bool escape = true) { while (len-- > 0) { char c = *str++; if (escape && (c == '\\')) { switch (c = *str++) { case 0 : case '0': c = 0; return; case 'a': c = '\a'; break; /* 07 */ case 'b': c = '\b'; break; /* 08 */ case 't': c = '\t'; break; /* 09 */ case 'n': c = '\n'; break; /* 0A */ case 'v': c = '\v'; break; /* 0B */ case 'f': c = '\f'; break; /* 0C */ case 'r': c = '\r'; break; /* 0D */ } } keychar(c); } } void keychar(char c) { static const unsigned char KEYCODES[] = { /* 00 */ KEY_RESERVED, /* 01 */ KEY_RESERVED, /* 02 */ KEY_RESERVED, /* 03 */ KEY_RESERVED, /* 04 */ KEY_RESERVED, /* 05 */ KEY_RESERVED, /* 06 */ KEY_RESERVED, /* 07 */ KEY_RESERVED, /* 08 */ KEY_RESERVED, /* 09 */ KEY_TAB, /* 0A */ KEY_LINEFEED, /* 0B */ KEY_RESERVED, /* 0C */ KEY_RESERVED, /* 0D */ KEY_ENTER, /* 0E */ KEY_RESERVED, /* 0F */ KEY_RESERVED, /* 10 */ KEY_RESERVED, /* 11 */ KEY_RESERVED, /* 12 */ KEY_RESERVED, /* 13 */ KEY_RESERVED, /* 14 */ KEY_RESERVED, /* 15 */ KEY_RESERVED, /* 16 */ KEY_RESERVED, /* 17 */ KEY_RESERVED, /* 18 */ KEY_RESERVED, /* 19 */ KEY_RESERVED, /* 1A */ KEY_RESERVED, /* 1B */ KEY_ESC, /* 1C */ KEY_RESERVED, /* 1D */ KEY_RESERVED, /* 1E */ KEY_RESERVED, /* 1F */ KEY_RESERVED, /* */ KEY_SPACE, /* ! */ KEY_1 | 0x80, /* " */ KEY_APOSTROPHE | 0x80, /* # */ KEY_3 | 0x80, /* $ */ KEY_4 | 0x80, /* % */ KEY_5 | 0x80, /* & */ KEY_7 | 0x80, /* ' */ KEY_APOSTROPHE, /* ( */ KEY_9 | 0x80, /* ) */ KEY_0 | 0x80, /* * */ KEY_8 | 0x80, /* + */ KEY_EQUAL | 0x80, /* , */ KEY_COMMA, /* - */ KEY_MINUS, /* . */ KEY_DOT, /* / */ KEY_SLASH, /* 0 */ KEY_0, /* 1 */ KEY_1, /* 2 */ KEY_2, /* 3 */ KEY_3, /* 4 */ KEY_4, /* 5 */ KEY_5, /* 6 */ KEY_6, /* 7 */ KEY_7, /* 8 */ KEY_8, /* 9 */ KEY_9, /* : */ KEY_SEMICOLON | 0x80, /* ; */ KEY_SEMICOLON, /* < */ KEY_COMMA | 0x80, /* = */ KEY_EQUAL, /* > */ KEY_DOT | 0x80, /* ? */ KEY_SLASH | 0x80, /* @ */ KEY_2 | 0x80, /* A */ KEY_A | 0x80, /* B */ KEY_B | 0x80, /* C */ KEY_C | 0x80, /* D */ KEY_D | 0x80, /* E */ KEY_E | 0x80, /* F */ KEY_F | 0x80, /* G */ KEY_G | 0x80, /* H */ KEY_H | 0x80, /* I */ KEY_I | 0x80, /* J */ KEY_J | 0x80, /* K */ KEY_K | 0x80, /* L */ KEY_L | 0x80, /* M */ KEY_M | 0x80, /* N */ KEY_N | 0x80, /* O */ KEY_O | 0x80, /* P */ KEY_P | 0x80, /* Q */ KEY_Q | 0x80, /* R */ KEY_R | 0x80, /* S */ KEY_S | 0x80, /* T */ KEY_T | 0x80, /* U */ KEY_U | 0x80, /* V */ KEY_V | 0x80, /* W */ KEY_W | 0x80, /* X */ KEY_X | 0x80, /* Y */ KEY_Y | 0x80, /* Z */ KEY_Z | 0x80, /* [ */ KEY_LEFTBRACE, /* \ */ KEY_BACKSLASH, /* ] */ KEY_RIGHTBRACE, /* ^ */ KEY_6 | 0x80, /* _ */ KEY_MINUS | 0x80, /* ` */ KEY_GRAVE, /* a */ KEY_A, /* b */ KEY_B, /* c */ KEY_C, /* d */ KEY_D, /* e */ KEY_E, /* f */ KEY_F, /* g */ KEY_G, /* h */ KEY_H, /* i */ KEY_I, /* j */ KEY_J, /* k */ KEY_K, /* l */ KEY_L, /* m */ KEY_M, /* n */ KEY_N, /* o */ KEY_O, /* p */ KEY_P, /* q */ KEY_Q, /* r */ KEY_R, /* s */ KEY_S, /* t */ KEY_T, /* u */ KEY_U, /* v */ KEY_V, /* w */ KEY_W, /* x */ KEY_X, /* y */ KEY_Y, /* z */ KEY_Z, /* { */ KEY_LEFTBRACE | 0x80, /* | */ KEY_BACKSLASH | 0x80, /* } */ KEY_RIGHTBRACE | 0x80, /* ~ */ KEY_GRAVE | 0x80, }; if (c < sizeof(KEYCODES) / sizeof(KEYCODES[0])) { c = KEYCODES[c]; if (c & 0x80) { keyctl(KEY_LEFTSHIFT, 1); keycode(c & ~0x80); keyctl(KEY_LEFTSHIFT, 0); } else if (c) keycode(c); } } void keycode(int code) { keyctl(code, 1); keyctl(code, 0); } void keyctl(int code, int value) { emit(EV_KEY, code, value); emit(EV_SYN, SYN_REPORT, 0); } private: void emit(int type, int code, int val) { struct input_event ie; ie.type = type; ie.code = code; ie.value = val; /* timestamp values below are ignored */ ie.time.tv_sec = 0; ie.time.tv_usec = 0; write(_fd, &ie, sizeof(ie)); } int _fd; }; class Input { public: Input(void) : _thid(0) , _fd(-1) , _enable(false) , _callback(nullptr) { } virtual ~Input(void) { stop(); } void callback(input_cb_t cb, void *arg = nullptr) { _callback = cb; _argument = arg; } bool start(const char *name1, const char *name2) { _name1 = name1; _name2 = name2; return _enable = (pthread_create(&_thid, NULL, run0, this) == 0); } void stop(void) { stop0(); _enable = false; } private: void stop0(void) { if (_fd != -1) { ioctl(_fd, EVIOCGRAB, 0); close(_fd); _fd = -1; } } static bool device(const char *name1, const char *name2, char *path) { int fd; DIR *dir; struct dirent *entry; char *p, device[128]; bool found = false; strcpy(path, "/sys/class/input/"); p = path + strlen(path); if ((dir = opendir(path))) { while((entry = readdir(dir))) { if ((entry->d_type == DT_LNK) && (strncmp(entry->d_name, "event", 5) == 0)) { sprintf(p, "%s/device/name", entry->d_name); device[0] = 0; if ((fd = open(path, O_RDONLY))) { read(fd, device, sizeof(device)); close(fd); } if (strstr(device, name1) && strstr(device, name2)) { sprintf(path, "/dev/input/%s", entry->d_name); found = true; break; } } } closedir(dir); } if (!found) *path = 0; return found; } void run(void) { char path[PATH_MAX]; struct input_event input; ssize_t n; while (_enable) { if (_fd != -1) { while ((n = read(_fd, &input, sizeof(input))) == sizeof(input)) { if (_callback) _callback(&input, _argument); } if (n <= 0) stop0(); } else { usleep(300000); // 300ms if (device(_name1.c_str(), _name2.c_str(), path)) { if ((_fd = open(path, O_RDONLY)) == -1) printf("[ERROR] open(\"%s\"): %s\n", path, strerror(errno)); else ioctl(_fd, EVIOCGRAB, 1); } } } } static void *run0(void *arg) { ((Input *)arg)->run(); return NULL; } std::string _name1; std::string _name2; pthread_t _thid; int _fd; bool _enable; input_cb_t _callback; void *_argument; }; class ABShutter3 { public: static const int BUTTON_A = KEY_VOLUMEUP; static const int BUTTON_B = KEY_ENTER; ABShutter3(void) : _callback(nullptr) , _argument(nullptr) , _buttun_b(0) { _input1.callback(input0, this); _input2.callback(input0, this); } virtual ~ABShutter3(void) { stop(); } void callback(input_cb_t cb, void *arg = nullptr) { _callback = cb; _argument = arg; } void start(void) { _input1.start("AB Shutter", "Keyboard"); // KEY_ENTER (28) _input2.start("AB Shutter", "Control" ); // KEY_VOLUMEUP (115) } void stop(void) { _input2.stop(); _input1.stop(); } private: void input(struct input_event *event) { if ((event->type == EV_KEY) && (event->value <= 1)) { // printf("code=%d, value=%d\n", event->code, event->value); if ((event->code == BUTTON_B) || (_buttun_b == 0)) { if (_callback) _callback(event, _argument); } if (event->code == BUTTON_B) _buttun_b = event->value; } } static void input0(struct input_event *event, void *arg) { ((ABShutter3 *)arg)->input(event); } Input _input1; Input _input2; input_cb_t _callback; void *_argument; int _buttun_b; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/* filepros.h - File Properties Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <stdio.h> #include "properties.h" class FileProps : public Properties { public: FileProps(void) { } virtual ~FileProps(void) { } bool load(const char* path) override { bool rv = false; FILE *fp = fopen(path, "r"); if (fp) { char buf[256]; clear(); rv = true; while (rv) { size_t len = fread(buf, 1, sizeof(buf), fp); if (len == 0) break; rv = parse(buf, len); } finish(); fclose(fp); } return rv; } bool save(const char* path, const char* header = nullptr) override { bool rv = false; FILE *fp = fopen(path, "w"); if (fp) { std::string str; toStringWithHeader(str, header); rv = fwrite(str.c_str(), 1, str.length(), fp) == str.length(); fclose(fp); } return rv; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
/* properties.h - Properties Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <vector> #include <string> #include <string.h> #include <stdlib.h> #include <stdint.h> class Properties { private: typedef struct { std::string name; std::string value; } property_t; typedef struct { std::string name; std::vector<property_t> properties; } section_t; std::vector<section_t> _sections; section_t _current; std::string _linebuf; protected: std::string& trim(std::string& str) { std::string::size_type pos; pos = str.find_first_not_of(' '); if (pos != std::string::npos) str.erase(0, pos); pos = str.find_last_not_of(' '); if (pos != std::string::npos) str.erase(pos + 1); return str; } section_t* getSection(const char* name) { if (name) { for (auto sec = _sections.begin(); sec != _sections.end(); ++sec) { if (strcasecmp(sec->name.c_str(), name) == 0) return &*sec; } } return nullptr; } void setSection(section_t &newSection) { section_t* sec = getSection(newSection.name.c_str()); if (!sec) _sections.push_back(newSection); else { for (auto prop = newSection.properties.begin(); prop != newSection.properties.end(); ++prop) setProperty(*sec, *prop); } } property_t* getProperty(section_t* section, const char* name) { if (section && name) { for (auto prop = section->properties.begin(); prop != section->properties.end(); ++prop) { if (strcasecmp(prop->name.c_str(), name) == 0) return &*prop; } } return nullptr; } void setProperty(section_t §ion, property_t &prop) { trim(prop.name); trim(prop.value); property_t* exists = getProperty(§ion, prop.name.c_str()); if (exists) exists->value = prop.value; else section.properties.push_back(prop); } bool parse(const char* buf, size_t len) { while (len--) { char c = *buf++; if (c >= ' ') _linebuf += c; else if (c == 0x0A) { trim(_linebuf); if (_linebuf.length()) { c = _linebuf[0]; if (c == '[') { if (_linebuf[_linebuf.length() - 1] != ']') return false; // format error. if (_current.properties.size() || _current.name.length()) setSection(_current); _current.name = _linebuf.substr(1, _linebuf.length() - 2); _current.properties.clear(); trim(_current.name); } else if ((c != ';') && (c != '#')) { int sep = _linebuf.find('='); if (sep < 0) sep = _linebuf.length(); property_t prop = {_linebuf.substr(0, sep), _linebuf.substr(sep + 1)}; setProperty(_current, prop); } _linebuf.clear(); } } } return true; } void finish(void) { if (_current.properties.size() || _current.name.length()) setSection(_current); _current.name.clear(); _current.properties.clear(); _linebuf.clear(); } public: Properties(void) { } virtual ~Properties(void) { } void clear(void) { _sections.clear(); _current.name.clear(); _current.properties.clear(); _linebuf.clear(); } std::vector<std::string>& getSectionNames(std::vector<std::string>& names) { names.clear(); for (auto sec = _sections.begin(); sec != _sections.end(); ++sec) names.push_back(sec->name); return names; } std::vector<std::string>& getPropertyNames(std::vector<std::string>& names, const char* section) { names.clear(); section_t* sec = getSection(section); if (sec) { for (auto prop = sec->properties.begin(); prop != sec->properties.end(); ++prop) names.push_back(prop->name); } return names; } void removeSection(const char* section) { if (section) { for (auto sec = _sections.begin(); sec != _sections.end(); ++sec) { if (strcasecmp(sec->name.c_str(), section) == 0) { _sections.erase(sec); break; } } } } void removeProperty(const char* section, const char* name) { if (name) { section_t* sec = getSection(section); if (sec) { for (auto prop = sec->properties.begin(); prop != sec->properties.end(); ++prop) { if (strcasecmp(prop->name.c_str(), name) == 0) { sec->properties.erase(prop); break; } } } } } void setProperty(const char* section, const char* name, const char* value) { if (section && name && value) { section_t newsec = {section, {{name, value}}}; setSection(newsec); } } void setPropertyInt(const char* section, const char* name, int value) { if (section && name) { char buf[16]; snprintf(buf, sizeof(buf), "%d", value); section_t newsec = {section, {{name, buf}}}; setSection(newsec); } } void setPropertyI64(const char* section, const char* name, int64_t value) { if (section && name) { char buf[128]; snprintf(buf, sizeof(buf), "%lld", value); section_t newsec = {section, {{name, buf}}}; setSection(newsec); } } const char* getProperty(const char* section, const char* name, const char* defval = "") { property_t* prop = getProperty(getSection(section), name); return prop ? prop->value.c_str() : defval; } int getPropertyInt(const char* section, const char* name, int defval = 0) { const char* value = getProperty(section, name); return *value ? atoi(value) : defval; } int getPropertyI64(const char* section, const char* name, int64_t defval = 0) { const char* value = getProperty(section, name); return *value ? strtoll(value, NULL, 10) : defval; } std::string& toString(std::string &str) { std::vector<std::string> names; std::string props; str.clear(); getSectionNames(names); for (auto name = names.begin(); name != names.end(); ++name) str.append("[").append(*name).append("]\n").append(toString(props, name->c_str())); return str; } std::string& toString(std::string &str, const char* section) { if (section == nullptr) toString(str); else { std::vector<std::string> names; str.clear(); getPropertyNames(names, section); for (auto name = names.begin(); name != names.end(); ++name) str.append(*name).append("=").append(getProperty(section, name->c_str())).append("\n"); } return str; } std::string& toStringWithHeader(std::string &str, const char* header) { str.clear(); if (header && *header) { const char* p; const char* q; p = q = header; do { if ((*q == 0) || (*q == 0x0A)) { str.append("# ").append(p, (size_t)q - (size_t)p).append("\n"); p = q + 1; } } while (*q++); } std::string props; return str.append(toString(props)); } virtual bool load(const char* path) = 0; virtual bool save(const char* path, const char* header = nullptr) = 0; }; |
【AB Shutter 3 ペアリング・スクリプト】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/bin/bash CWD=$(cd $(dirname $0) && pwd) SCAN=${CWD}/ble-scan PAIRING=${CWD}/ble-pairing bluetoothctl power off bluetoothctl power on bluetoothctl agent NoInputNoOutput if [ -f ${PAIRING} ]; then MAC=$(cat ${PAIRING}) if [ "${MAC}" != "" ]; then bluetoothctl disconnect ${MAC} bluetoothctl untrust ${MAC} bluetoothctl remove ${MAC} fi rm ${PAIRING} > /dev/null 2>&1 fi stdbuf -o0 bluetoothctl scan on 1> ${SCAN} & while true do sleep 1 MAC=$(sed -n -r "s/\[.*NEW.*\] Device (.*) AB Shutter/\1/p" ${SCAN} | head -n 1) if [ "${MAC}" != "" ]; then break; fi done echo ${MAC} > ${PAIRING} bluetoothctl pair ${MAC} bluetoothctl trust ${MAC} bluetoothctl connect ${MAC} |