2024/10/08

LINE Notify 的替代方案

 LINE Notify 割韭菜了.

只好拿稍微麻煩一點的 pushbullet 來用.

產生 token:

登入 pushbullet 主頁.


在帳號的設定頁面建立 token.



請牢記你的 token, 保管好並且不要洩漏.
然後點選你想接收訊息的裝置

URL 最後就是你的裝置 ID.
接下來就是透過 curl 傳遞我們想要傳送的訊息:
curl -X POST https://api.pushbullet.com/v2/pushes \
-H "Authorization: Bearer ${你的token}>" \
-H "Content-Type: application/json" \
-d "{\"type\": \"note\", \"title\": \"${訊息抬頭}\", \"body\": \"${訊息內容}\", \"device_iden\": \"${你的裝置ID}\"}"



2024/04/18

[C] chek cpu endian

1
2
3
4
5
6
7
8
#include <stdint.h>

static uint8_t is_little_endian(void)
{
    uint16_t value = 0x0001;
    uint8_t *byte_ptr = (uint8_t *)&value;
    return (*byte_ptr == 0x01); // If the system is little endian, the least significant byte is 0x01.
}


2024/04/09

[C] Create a folder even if the parent folder does not exist.

 

 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
static int my_dirname(char *path) {
    char *p = strrchr(path, '/');
    if (p == NULL || p == path) {
        return -1;
    }
    *p = '\0';
    return 0;
}

static int my_mkdir(char *path) {
    struct  stat    st;
    char            p[128];
    char            l[128];

    mkdir(path, 0775);
    while ((stat(path, &st) != 0)) {
        memset(p, 0, 128);
        memcpy(p, path, strlen(path));
        if (0 != my_dirname(p)) {
            return (-1);
        }
        while ((stat(p, &st) != 0)) {
            memset(l, 0, 128);
            memcpy(l, p, strlen(p));  // save the last not exist parent path.
            if (0 != my_dirname(p)) {
                return (-1);
            }
        }
        mkdir(l, 0775);
        if (stat(l, &st) != 0) {
            return (-1);
        }
        mkdir(path, 0775);
    }
    return (0);
}

 

2023/06/20

Use tc and netem to simulate network delay, jitter and packet loss.

netem relies on kernel module:
CONFIG_IFB
CONFIG_NET_SCHED
CONFIG_NET_SCH_NETEM.

tc qdisc add dev br-lan root handle 1: htb

  1. This will create a root qdisc on the br-lan interface for htb

tc class add dev br-lan parent 1: classid 1:1 htb rate 1000mbit
tc qdisc add dev br-lan parent 1:1 netem delay 100ms 10ms
tc filter add dev br-lan parent 1:0 protocol ip u32 match ip sport 5101 0xffff flowid 1:1

  1. Create a class 1:1 under 1: of br-lan and set the speed limit to 1000bps.
  2. For class 1:1 set its netem to delay 100ms and jitter 10ms.
  3. Direct the packet with src port 5101 of the br-lan interface to class 1:1.

tc class add dev br-lan parent 1: classid 1:2 htb rate 1000mbit
tc qdisc add dev br-lan parent 1:2 netem loss 0% duplicate 0% delay 30ms 20ms
tc filter add dev br-lan parent 1:0 protocol ip u32 match ip sport 5001 0xffff flowid 1:2

  1. Create a class 1:2 under 1: of br-lan and set the speed limit to 1000bps.
  2. For class 1:2 set its netem to loss 0%, duplicate 0%, delay 30ms and jitter 20ms.
  3. Direct the packet with src port 5001 of the br-lan interface to class 1:2.

2023/04/21

[C] 16進制字串與 byte 陣列戶轉

 Convert between hex string and byte array.

以空間換取時間, 盡量減少計算跟比較.

 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
static uint8_t HEX_VALUE[256] = {
//  x0 x1 x2 x3 x4 x5 x6 x7   x8 x9 xA xB xC xD xE xF
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 0x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 1x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 2x
     0, 1, 2, 3, 4, 5, 6, 7,   8, 9, 0, 0, 0, 0, 0, 0,  // 3x
     0,10,11,12,13,14,15, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 4x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 5x
     0,10,11,12,13,14,15, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 6x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 7x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 8x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 9x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Ax
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Bx
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Cx
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Dx
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Ex
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0   // Fx
};

static uint8_t HEX_CHAR[16] = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

void bytes2hex(uint8_t* src, char* dest, uint8_t srcsize) {
    uint8_t flag;
    uint8_t pos;

    flag = 0;
    pos = 0;
    while (srcsize--) {
if (flag || src[srcsize]) {
dest[pos++] = HEX_CHAR[src[srcsize]>>4];
dest[pos++] = HEX_CHAR[src[srcsize]&0xf];
} } } void hex2bytes(char *src, uint8_t* dest, uint8_t destsize) { uint8_t len; uint8_t pos; uint8_t hilo; uint8_t byte; uint8_t lohi[2]; char *cptr; len = strlen(src); if (len > (destsize* 2)) {
return; } cptr = src + len; pos = 0; ++len; while (len) { memset(lohi, 0, 2); hilo = 2; while (hilo-- && --len) { lohi[hilo] = HEX_VALUE[*--cptr]; } dest[pos++] = lohi[1] | (lohi[0] << 4); } return; }