Skip to content

Instantly share code, notes, and snippets.

@tai
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save tai/d6410831483b3ff75816 to your computer and use it in GitHub Desktop.

Select an option

Save tai/d6410831483b3ff75816 to your computer and use it in GitHub Desktop.
/*BINFMTC:-Wall -std=gnu99 -I/d/src/wireshark-1.12.1+g01b65bf -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lpcap
*
* Test to generate dummy pcap data for I2C communication.
*
* Usage:
* $ ./pcapgen-i2c.c hoge.pcap
* $ tshark -V -d wtap_encap==103,i2c -r hoge.pcap
*
* NOTE:
* Not sure where WTAP_ENCAP value of 103 came from.
* I had to use something for -d option to apply "decode-as" function, and
* just happen to find that it can be used.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <endian.h>
#include <pcap/pcap.h>
#include <wiretap/wtap.h>
/*
* I2C link-layer on-disk format - from wiretap/pcap-common.c
*/
struct i2c_file_hdr {
guint8 bus;
guint8 flags[4];
};
int
main(int argc, char **argv) {
pcap_dumper_t *pd;
pcap_t *pc;
struct {
struct i2c_file_hdr header;
uint8_t payload[1];
} frame;
int snaplen = sizeof(frame);
pc = pcap_open_dead(DLT_IPMB, snaplen);
pd = pcap_dump_open(pc, argv[1]);
char msg[] = "hello, pcap";
for (int i = 0; i < sizeof(msg); i++) {
frame.payload[0] = msg[i];
//
// 0b00000000
// ^event bit
// ^^^^^^^bus address
//
frame.header.bus = 0x17; // dummy address
//
// 0xaaaabbbb
// ^^^^I2C_EVENT_ERR_* bits
// ^^^^if is_event: I2C_EVENT_* non-ERR bits
// else: I2C_FLAG_* bits
//
memset(frame.header.flags, 0, 4);
struct pcap_pkthdr ph;
// add timestamp
gettimeofday(&ph.ts, NULL);
// captured caplen out of len bytes
ph.caplen = sizeof(frame);
ph.len = sizeof(frame);
pcap_dump((u_char *)pd, &ph, (u_char *)&frame);
}
pcap_dump_close(pd);
pcap_close(pc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment