aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmtp/mtp.c
blob: 02afbddeaee99a6f286d093239ac8be438e98c71 (plain)
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
/* MTP common functions
 *
 * (C) 2020 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define CHAN mtp->name

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "../libtimer/timer.h"
#include "../libdebug/debug.h"
#include "mtp.h"

static void mtp_t1(struct timer *timer)
{
        mtp_t *mtp = (mtp_t *)timer->priv;

	mtp_send(mtp, MTP_PRIM_T1_TIMEOUT, 0, NULL, 0);
}

static void mtp_t2(struct timer *timer)
{
        mtp_t *mtp = (mtp_t *)timer->priv;

	mtp_send(mtp, MTP_PRIM_T2_TIMEOUT, 0, NULL, 0);
}

static void mtp_t3(struct timer *timer)
{
        mtp_t *mtp = (mtp_t *)timer->priv;

	mtp_send(mtp, MTP_PRIM_T3_TIMEOUT, 0, NULL, 0);
}

static void mtp_t4(struct timer *timer)
{
        mtp_t *mtp = (mtp_t *)timer->priv;

	mtp_send(mtp, MTP_PRIM_T4_TIMEOUT, 0, NULL, 0);
}

int mtp_init(mtp_t *mtp, const char *name, void *inst, void (*mtp_receive)(void *inst, enum mtp_prim prim, uint8_t slc, uint8_t *data, int len), int bitrate, int ignore_monitor, uint8_t sio, uint16_t local_pc, uint16_t remote_pc)
{
	memset(mtp, 0, sizeof(*mtp));

	mtp->name = name;
	mtp->inst = inst;
	mtp->mtp_receive = mtp_receive;
	if (bitrate != 64000 && bitrate != 4800) {
		fprintf(stderr, "Wrong bit rate %d, please fix!\n", bitrate);
		abort();
	}
	mtp->bitrate = bitrate;
	mtp->ignore_monitor = ignore_monitor;
	mtp->sio = sio;
	mtp->local_pc = local_pc;
	mtp->remote_pc = remote_pc;
	timer_init(&mtp->t1, mtp_t1, mtp);
	timer_init(&mtp->t2, mtp_t2, mtp);
	timer_init(&mtp->t3, mtp_t3, mtp);
	timer_init(&mtp->t4, mtp_t4, mtp);

	return 0;
}

void mtp_exit(mtp_t *mtp)
{
	if (!mtp)
		return;

	timer_exit(&mtp->t1);
	timer_exit(&mtp->t2);
	timer_exit(&mtp->t3);
	timer_exit(&mtp->t4);

	mtp_flush(mtp);
}

void mtp_flush(mtp_t *mtp)
{
	struct mtp_msg *temp;

	while (mtp->tx_queue) {
		temp = mtp->tx_queue;
		mtp->tx_queue = mtp->tx_queue->next;
		free(temp);
	}
}