2023-03-10

인터넷연결 확인방법

단순히 인터넷이 되는지만 확인하고자 한다면,

DNS서버에 접속만 확인한다.

PING은 지연시간까지 표시해주지만 필요없다. 복잡도도 상당하다.

8.8.8.8 주소, 53번포트에 connect만 하여 성공실패여부만 확인하고 끝.

소스는 리눅스에서는 바로되고, 필요한만큼만, 수정해서 쓰자.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <hostname> <port>\n", argv[0]);
exit(1);
}

char *hostname = argv[1];
char *port = argv[2];
struct addrinfo hints, *res;
int status, sock;

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if ((status = getaddrinfo(hostname, port, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}

sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

if (connect(sock, res->ai_addr, res->ai_addrlen) == 0) {
printf("Success\n");
} else {
printf("Failed\n");
}

close(sock);
freeaddrinfo(res);
return 0;
}


2023-03-08

[tcp] multicast reciever

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MULTICAST_ADDR "239.0.0.1"
#define PORT 8888
#define BUFSIZE 1024

int main(int argc, char** argv) {
int sock;
struct sockaddr_in addr;
char buf[BUFSIZE];
int recv_len;
struct ip_mreq mreq;
int enable = 1;

// 멀티캐스트 소켓 생성
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}

// 멀티캐스트 TTL 설정
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &enable, sizeof(enable));

// 멀티캐스트 그룹에 참여
mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_ADDR);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

// 소켓 주소 설정
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(PORT);

// 소켓 바인드
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Failed to bind socket");
exit(EXIT_FAILURE);
}

// 데이터 수신 및 출력
while (1) {
memset(buf, 0, BUFSIZE);
recv_len = recv(sock, buf, BUFSIZE, 0);
if (recv_len < 0) {
perror("Failed to receive data");
exit(EXIT_FAILURE);
}
printf("Received data: %s\n", buf);
}

// 소켓 닫기
close(sock);

return 0;
}

[udp] multicast send

//// sender
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MULTICAST_ADDR "239.0.0.1"
#define PORT 8888
#define FILENAME "test.txt"

int main(int argc, char **argv)
{
int sock;
struct sockaddr_in addr;
char buf[1024];
int read_len, send_len, total_len;
FILE *fp;
struct ip_mreq mreq;
int enable = 1;

// 파일 열기
fp = fopen(FILENAME, "r");
if (fp == NULL)
{
perror("Failed to open file");
exit(EXIT_FAILURE);
}

// 멀티캐스트 소켓 생성
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
perror("Failed to create socket");
exit(EXIT_FAILURE);
}

// 멀티캐스트 TTL 설정
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &enable, sizeof(enable));

// 멀티캐스트 그룹에 참여
mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_ADDR);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

// 소켓 주소 설정
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(MULTICAST_ADDR);
addr.sin_port = htons(PORT);

// 파일 읽어서 멀티캐스트로 전송
while ((read_len = fread(buf, 1, sizeof(buf), fp)) > 0)
{
total_len = 0;
while (total_len < read_len)
{
send_len = sendto(sock, buf + total_len, read_len - total_len, 0,
(struct sockaddr *)&addr, sizeof(addr));
if (send_len < 0)
{
perror("Failed to send data");
exit(EXIT_FAILURE);
}
total_len += send_len;
}
}

// 파일과 소켓 닫기
fclose(fp);
close(sock);

return 0;
}

2023-03-01

[nmcli] mac ip match

sudo nmcli connection modify eth0 ipv4.method shared
sudo nmcli connection modify eth0 ipv4.dhcp-client-id "my_client_id"
sudo nmcli connection modify eth0 ipv4.dhcp-timeout 120
sudo nmcli connection modify eth0 ipv4.dhcp-hostname "my_hostname"
sudo nmcli connection modify eth0 ipv4.dhcp-lease-time 3600
sudo nmcli connection modify eth0 ipv4.dhcp-send-hostname yes
sudo nmcli connection modify eth0 ipv4.dhcp-send-client-id yes
sudo nmcli connection modify eth0 ipv4.dhcp-hostname "my_hostname"
sudo nmcli connection modify eth0 +ipv4.dhcp-hostname-mac 00:11:22:33:44:55,192.168.1.50
sudo nmcli connection modify eth0 +ipv4.dhcp-hostname-mac 00:AA:BB:CC:DD:EE,192.168.1.51

2023-02-28

[docker] portainer

$ docker volume create portainer_data
$ docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /data/portainer:/data portainer/portainer-ce:latest

2023-02-27

[docker] maclvan

1. macvlan을 생성한다.
MACVLAN_PARENT_INTERFACE=enx047bcb5e8b20
MACVLAN_SUBNET=192.168.25.0/24
MACVLAN_GATEWAY=192.168.25.1
docker network create -d macvlan   --subnet=$MACVLAN_SUBNET   --gateway=$MACVLAN_GATEWAY   -o parent=$MACVLAN_PARENT_INTERFACE   macvlan_network

#고정IP
docker run -itd --name=macvlan_container --network=macvlan_network --ip=192.168.25.100 --mac-address=00:11:22:33:44:55 hello:0.1

#자동IP
docker run -it --name=macvlan_container --network=macvlan_network --mac-address=00:11:22:33:44:55 hello:0.1


[BLE] 다이소 ble모듈 리눅스에 연결시... 해결법.

[46864.573210] usb 3-3.4.3.4.3: new full-speed USB device number 15 using xhci_hcd
[46864.682163] usb 3-3.4.3.4.3: New USB device found, idVendor=0bda, idProduct=8771, bcdDevice= 2.00
[46864.682171] usb 3-3.4.3.4.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[46864.682173] usb 3-3.4.3.4.3: Product: Bluetooth Radio
[46864.682174] usb 3-3.4.3.4.3: Manufacturer: Realtek
[46864.682176] usb 3-3.4.3.4.3: SerialNumber: 00E04C239987
[46864.692638] Bluetooth: hci1: RTL: examining hci_ver=0a hci_rev=000b lmp_ver=0a lmp_subver=8761
[46864.693653] Bluetooth: hci1: RTL: rom_version status=0 version=1
[46864.693660] Bluetooth: hci1: RTL: loading rtl_bt/rtl8761bu_fw.bin
[46864.703096] bluetooth hci1: Direct firmware load for rtl_bt/rtl8761bu_fw.bin failed with error -2
[46864.703106] Bluetooth: hci1: RTL: firmware file rtl_bt/rtl8761bu_fw.bin not found


$ cd /usr/lib/firmware/rtl_bt $ sudo ln -s rtl8761b_fw.bin rtl8761bu_fw.bin


2023-02-17

[팁] ssh접속시 다 보내기.

#접속만 하고싶다. sshpass -p 'P@ssw0rd' ssh -o StrictHostKeyChecking=no root@1.2.3.4 #파일복사 하고싶다. sshpass -p 'P@ssw0rd' scp -o StrictHostKeyChecking=no * root@remotsvr.local:/usr/local/fw/ [원문] https://zetawiki.com/wiki/%EB%A6%AC%EB%88%85%EC%8A%A4_sshpass_%EC%82%AC%EC%9A%A9%EB%B2%95 

2023-02-14

[개발노트] embedded linux vscode remote gdb

vscode로 편집만 하던 리눅스 app을 원격 디버깅 해보기.

sudo apt install build-essential gdb gdb-multiarch

vscode 로 폴더 열기.

.vscode 폴더에 3개의 파일 만들기.

launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "GDB-Debug",
"type": "cppdbg",
"request": "launch",
"program": "${config:BINARY}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "integratedTerminal",
"MIMode": "gdb",
"targetArchitecture": "arm",
"preLaunchTask": "deploy",
"setupCommands": [
{
"description": "Pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true,
}
],
"miDebuggerPath": "/usr/bin/gdb-multiarch",
"miDebuggerServerAddress": "${config:TARGET_IP}:${config:DEBUG_PORT}",
}
]
}

settings.json

{
"TARGET_IP":"192.168.25.53",
"DEBUG_PORT":"6666",
"BINARY":"s1app3",

"SDK_DIR":"/home/happycpu/work/wrlinux/sdk/environment-setup-cortexa7t2hf-neon-vfpv4-wrs-linux-gnueabi",
}

tasks.json

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "cross-compile",
"type": "shell",
"command": "sh",
"args": [
"cross-compile.sh",
"${config:SDK_DIR}",
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "deploy",
"isBackground": true,
"type": "shell",
"command": "sh",
"args": [
"deploy.sh",
"${config:TARGET_IP}",
"${config:DEBUG_PORT}",
"${config:BINARY}"
],
"problemMatcher": {
"base": "$gcc",
"background": {
"activeOnStart": true,
"beginsPattern": "${config:BINARY}",
"endsPattern": "Listening on port [0-9]{4}"
}
},
"dependsOn": [
"cross-compile"
],
},
]
}


스크립트파일2개 추가.

cross-compile.sh

#!/bin/bash
SDK_DIR="$1"

. ${SDK_DIR}
cmake .
make clean; make -j$(nproc)

*yocto 의 sdk를 source 하여주는 행위와 cmake파일 생성하고 make까지 함.


deploy.sh

#!/bin/bash
DEST_IP="$1"
DEBUG_PORT="$2"
BINARY="$3"
DEST_DIR="/root"

# kill gdbserver on tx8m and delete old binary
ssh root@${DEST_IP} "sh -c '/usr/bin/killall -q gdbserver; rm -rf ${DEST_DIR}/${BINARY} exit 0'"

# send binary to target
scp ${BINARY} root@${DEST_IP}:${DEST_DIR}/${BINARY}

# start gdbserver on target
ssh -t root@${DEST_IP} "sh -c 'cd ${DEST_DIR}; gdbserver localhost:${DEBUG_PORT} ${BINARY}'"

*DEST_DIR은 타겟보드의 실행파일 위치.


[원문]https://karo-electronics.github.io/docs/software-documentation/vscode/index.html#





2023-02-03

[yocto embedded linux dotnet azure-iot-sdk-charp] 절차

[머신작업]
yocto에는 meta-dotnet-core를 추가해준다.
$ build/conf/bblayers.conf 에 추가함.
나의 욕토는 hardknott이므로, 추가해준다. 
meta-dotnet-core/conf/layer.conf 
LAYERSERIES_COMPAT_dotnet-core = "honister dunfell kirkstone hardknott"
던펠까지만 있었으면, 상위버전이므로 안될수도있었는데, kirkstone까지 되는것이므로 그 아랫버전은 될것이라 생각하고 했더니 되더라.
이미지에 dotnet-core 추가. 추가 위치는 나의 이미지 어펜드들 모아놓은곳에 추가함.
빌드하면 이미지가 약 100메가정도 늘어남.
추가로, master는 dotnet7.0이 기본이므로, pc의 개발환경과 azure sample소스등이 dotnet60으로 된것을 고려하여 타겟의 버전도 6.0으로 찍어준다.


build command for linux-arm

dotnet publish -r linux-arm -p:PublishSingleFile=true --self-contained false -c Release



2023-02-02

[쉘] 바이너리 파일에서 추출하여 버전빼기


grep -a "\$Revision.*\$" /usr/local/network_adapter.bin 를 실행하면 아래와 같이 추출해준다.
$Id: ESP32 $ $Date: Feb  1 2023 22:28:54 $ $Revision: 0.0.5.std $

이걸 이용해서 추가해서 0.0.5.std 만 추출하고싶다.
grep -a "\$Revision.*\$" /usr/local/network_adapter.bin | cut -d "$" -f6 | cut -d " " -f2
0.0.5.std


암호화 + base64 하고 쉘스크립트.

 #!/bin/bash # IV와 UserKey를 설정합니다 (아스키 문자열을 그대로 사용). IV="0123456789abcdef"  UserKey="0123456789abcdef" # IV와 UserKey를 16...